概述
状态栏一体化
4.4及以下
不推荐使用,好多都是自定义SystemStatusManager,或者根据SDK版本加载不同的布局
5.0以上
由于5.0引入了 Material Design ,状态栏对开发者更加直接,可以直接调用 setStatusBarColor 来设置状态栏的颜色。
程序代码
getWindow().setStatusBarColor(statusBarColor());
style配置
@color/status_bar_color
状态栏文字颜色改变
状态栏文字颜色只能在6.0及以上版本才能改变,而且只有两种颜色,默认颜色(白色)和淡色(灰色)。
程序代码
/**
* 设置状态栏颜色是否是灰色(默认白色和灰色)
*
* @param isGray
*/
public void setStatueTextColor(boolean isGray) {
if (isGray) {
// 灰色
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
// 白色
getWindow().getDecorView().setSystemUiVisibility(0);
}
}
style配置
false
true:淡色(灰色) false:默认颜色(白色)
例子代码
下面我以动态改变状态栏颜色及文字为例,该例子基于6.0及以上版本(由于状态栏文字6.0及以上才能改变)。
1、style配置
true
@style/ActivityAnimation
2、父类BaseActivity.java
抽象方法,获取子类页面设置的状态栏颜色
public abstract int statusBarColor();
// 设置状态栏和状态栏文字颜色(6.0)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int color = statusBarColor();
getWindow().setStatusBarColor(color);
//状态栏为白色时,状态栏字体为灰色,其他都是默认颜色
if (color == Color.WHITE) {
setStatueTextColor(true);
} else {
setStatueTextColor(false);
}
}
/**
* 设置状态栏颜色是否是灰色(默认白色和灰色)
*
* @param isGray
*/
public void setStatueTextColor(boolean isGray) {
if (isGray) {
// 灰色
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
// 白色
getWindow().getDecorView().setSystemUiVisibility(0);
}
}
3、子类MainActivity.java
布局文件
android:id="@+id/main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/activity_bg"
android:fitsSystemWindows="true">
layout="@layout/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:id="@+id/main_left_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#01AAE1"
android:fitsSystemWindows="true">
layout="@layout/main_left_menu"
android:layout_width="match_parent"
android:layout_height="match_parent" />
注意点:android:fitsSystemWindows="true"和侧边栏的 android:background="#01AAE1"
抽象实现
/**
* 侧滑栏效果所以设为全透明
*/
@Override
public int statusBarColor() {
return Color.TRANSPARENT;
}
动态设置
drawerLayout.setStatusBarBackgroundColor(Color.WHITE);
setStatueTextColor(true);
drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
}
@Override
public void onDrawerOpened(View drawerView) {
setStatueTextColor(false);
}
@Override
public void onDrawerClosed(View drawerView) {
setStatueTextColor(true);
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
例子效果图
正常效果
展开效果