状态栏背景颜色以及字体颜色的更改

安卓沉浸式是4.4以后才有官方api,我找到了一个依赖库可以很简便的设置状态栏的颜色和字体的颜色,不足就是只有在4.4以后有效。下面我介绍一下使用方法:

build.gradle中引入:

compile'com.githang:status-bar-compat:0.5'

setContentView()后面添加

StatusBarCompat.setStatusBarColor(this, color, lightStatusBar);

其中color参数就是状态栏的背景颜色,lightStatusBar的类型是boolean,true表示的就是状态栏中的字体图标颜色都是深色,而false就是相反是白色。设置了之后就完成了沉浸式界面。

文章来源:https://github.com/msdx/status-bar-compat

在这里我推荐另一种方式:

使用

1.在 build.gradle 文件中添加依赖, StatusBarUtil 已经发布在 JCenter:

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

2.在 setContentView() 之后调用你需要的方法,例如:

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

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>

滑动返回界面设置状态栏颜色:

建议配合 bingoogolapple/BGASwipeBackLayout-Android: Android Activity 滑动返回 库一起使用。

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

当你设置了 statusBarAlpha 值时,该值需要在 0 ~ 255 之间

在 Fragment 中的使用可以参照 UseInFragmentActivity.java 来实现

项目 GitHub 地址

文章来源:https://jaeger.itscoder.com/android/2016/03/27/statusbar-util.html

猜你喜欢

转载自blog.csdn.net/qq_36437339/article/details/80860823