第三方库ImmersionBar
第三方库StatusBarUtil
特性
设置状态栏颜色
1
StatusBarUtil.setColor(Activity activity, int color)
设置状态栏半透明
1
StatusBarUtil.setTranslucent(Activity activity, int statusBarAlpha)
设置状态栏全透明
1
StatusBarUtil.setTransparent(Activity activity)
为包含
DrawerLayout
的界面设置状态栏颜色(也可以设置半透明和全透明)1
StatusBarUtil.setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, int color)
为使用 ImageView 作为头部的界面设置状态栏透明
1
StatusBarUtil.setTranslucentForImageView(Activity activity, int statusBarAlpha, View needOffsetView)
在 Fragment 中使用
为滑动返回界面设置状态栏颜色
推荐配合 bingoogolapple/BGASwipeBackLayout-Android: Android Activity 滑动返回 这个库一起使用。
1
StatusBarUtil.setColorForSwipeBack(Activity activity, @ColorInt int color, int statusBarAlpha)
通过传入
statusBarAlpha
参数,可以改变状态栏的透明度值,默认值是112。
使用
在 build.gradle 文件中添加依赖, StatusBarUtil 已经发布在 JCenter:
1
compile 'com.jaeger.statusbarutil:library:1.4.0'
在
setContentView()
之后调用你需要的方法,例如:1
2
3setContentView(R.layout.main_activity);
...
StatusBarUtil.setColor(MainActivity.this, mColor);如果你在一个包含
DrawerLayout
的界面中使用, 你需要在布局文件中为DrawerLayout
添加android:fitsSystemWindows="true"
属性:1
2
3
4
5
6
7
8
9
10
11<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>滑动返回界面设置状态栏颜色:
建议配合 bingoogolapple/BGASwipeBackLayout-Android: Android Activity 滑动返回 库一起使用。
1
StatusBarUtil.setColorForSwipeBack(Activity activity, @ColorInt int color, int statusBarAlpha)
当你设置了
statusBarAlpha
值时,该值需要在 0 ~ 255 之间在 Fragment 中的使用可以参照 UseInFragmentActivity.java 来实现
SystemBarTintManager状态栏透明
1 | import android.annotation.SuppressLint; |
在基类中 onCreate()中调用 setStatusBar();
1 | protected void setStatusBar(){ |