对安卓应用开发者,UI部分最难搞的就是APP顶部的状态栏。
一致的APP风格,状态栏仅仅需要设置一种颜色。
安卓全局颜色设置
单一APP状态栏
很显然,我们只要将AppTheme中colorPrimaryDark修改为APP设计中的统一风格即可。
动态修改APP状态栏颜色
修改APP Theme
主要是window的一些配置
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowNoTitle">true</item>
<item name="windowNoTitle">true</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowBackground">@android:color/holo_blue_bright</item>
</style>
运行,看一下效果
状态栏遮挡了APP"正文"
在activity布局文件 根布局添加
android:fitsSystemWindows="true"
动态修改状态栏背景颜色
getWindow().getDecorView().setBackgroundResource(android.R.color.holo_green_dark);
这样就可以动态修改状态栏背景颜色。
修改状态栏图标颜色
如果状态栏背景修改为白色,那原来的状态栏图标岂不是都不可见了?
像这样什么都看不到
将白色修改为黑色(PS.似乎只能是这两种颜色)
Window window = getWindow();
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
如果需要将状态栏图标颜色动态修改为白色怎么处理呢?
getWindow().getDecorView().setSystemUiVisibility(0);
小米9SE测试ok,至于其他的版本,请自行确认。