1. 程式人生 > >更改沉浸式狀態列非常好用的一個工具類 StatusBarUtil

更改沉浸式狀態列非常好用的一個工具類 StatusBarUtil

1.設定狀態列顏色

StatusBarUtil.setColor(Activity activity, int color)

2.設定狀態列半透明

StatusBarUtil.setTranslucent(Activity activity, int statusBarAlpha)

3.設定狀態列全透明

StatusBarUtil.setTransparent(Activity activity)

4.為包含 DrawerLayout 的介面設定狀態列顏色(也可以設定半透明和全透明

StatusBarUtil.setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, int color)

5.為使用 ImageView 作為頭部的介面設定狀態列透明

StatusBarUtil.setTranslucentForImageView(Activity activity, int statusBarAlpha, View needOffsetView)

6.為滑動返回介面設定狀態列顏色 推薦配合 bingoogolapple/BGASwipeBackLayout-Android: Android Activity 滑動返回 這個庫一起使用。

StatusBarUtil.setColorForSwipeBack(Activity activity, @ColorInt int color, int statusBarAlpha)

7.通過傳入 statusBarAlpha 引數,可以改變狀態列的透明度值,預設值是112。

使用

7.1在 build.gradle 檔案中新增依賴, StatusBarUtil 已經發布在 JCenter:

compile 'com.jaeger.statusbarutil:library:1.4.0'

7.2在 setContentView() 之後呼叫你需要的方法,例如:

setContentView(R.layout.main_activity);
...
StatusBarUtil.setColor(MainActivity.this, mColor);

7.3如果你在一個包含 DrawerLayout 的介面中使用, 你需要在佈局檔案中為 DrawerLayout 新增 android:fitsSystemWindows="true" 屬性:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    ...

</android.support.v4.widget.DrawerLayout>

7.4滑動返回介面設定狀態列顏色:

建議配合 bingoogolapple/BGASwipeBackLayout-Android: Android Activity 滑動返回 庫一起使用。

StatusBarUtil.setColorForSwipeBack(Activity activity, @ColorInt int color, int statusBarAlpha)

7.5當你設定了 statusBarAlpha 值時,該值需要在 0 ~ 255 之間

7.6在 Fragment 中的使用可以參照 UseInFragmentActivity.java 來實現

最後附上文章原地址 https://jaeger.itscoder.com/android/2016/03/27/statusbar-util.html