最近app為了跟ios一致,要做沉浸式狀態欄,所以聽說了這個庫,為了以后再用,所以轉載。這位大佬厲害。大佬的地址: github 博文原地址 借個樓,根據他的代碼,我寫了個類似輪播圖片的沉浸式狀態欄,地址
這是一個為Android App 設置狀態欄的工具類, 可以在4.4及其以上系統中實現 沉浸式狀態欄/狀態欄變色,支持設置狀態欄透明度,滿足你司設計師的各種要求(霧)。
在此之前我寫過一篇Android App 沉浸式狀態欄解決方案,后來我司設計師說默認的透明度太深了,讓我改淺一點,然后在想了一些辦法之后給解決了。本著不重復造輪子的原則,索性整理成一個工具類,方便需要的開發者。
設置狀態欄顏色
StatusBarUtil.setColor(Activity activity, int color)設置狀態欄半透明
StatusBarUtil.setTranslucent(Activity activity, int statusBarAlpha) ? ?
設置狀態欄全透明
StatusBarUtil.setTransparent(Activity activity)
為包含 DrawerLayout 的界面設置狀態欄顏色(也可以設置半透明和全透明) StatusBarUtil.setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, int color)
為使用 ImageView 作為頭部的界面設置狀態欄透明 StatusBarUtil.setTranslucentForImageView(Activity activity, int statusBarAlpha, View needOffsetView)
在 Fragment 中使用
為滑動返回界面設置狀態欄顏色
推薦配合 bingoogolapple/BGASwipeBackLayout-Android: Android Activity 滑動返回 這個庫一起使用。 StatusBarUtil.setColorForSwipeBack(Activity activity, @ColorInt int color, int statusBarAlpha)
通過傳入 statusBarAlpha 參數,可以改變狀態欄的透明度值,默認值是112。
使用 在 build.gradle 文件中添加依賴, StatusBarUtil 已經發布在 JCenter: compile 'com.jaeger.statusbaruitl:library:1.3.5'
在 setContentView() 之后調用你需要的方法,例如:
setContentView(R.layout.main_activity); ...
StatusBarUtil.setColor(MainActivity.this, mColor);
如果你在一個包含 DrawerLayout 的界面中使用, 你需要在布局文件中為 DrawerLayout 添加 android:fitsSystemWindows=”true” 屬性:
新聞熱點
疑難解答