更改沉浸式状态栏非常好用的一个工具类 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

猜你喜欢

转载自blog.csdn.net/mintent/article/details/84767198