1.修改状态栏颜色,即修改application主题theme 或者 activity主题theme
values/styles/
<!--状态栏颜色 样式 -->
<style name="TranslucentTheme" parent="AppTheme"> </style>
values-v19/styles
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar"> //设置为true,意味着xml布局延伸到状态栏 <item name="android:windowTranslucentStatus" >true</item> <item name="android:windowTranslucentNavigation" >false</item> </style>
values-v21/styles
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar"> //设置为false,意味着布局不会延伸到状态栏 <item name="android:windowTranslucentStatus" >false</item> <item name="android:windowTranslucentNavigation">false</item> <!--Android 5.x开始需要把颜色设置为程序的主题色白色,否则导航栏会呈现系统默认的浅灰色--> <item name="android:statusBarColor">@android:color/white</item> </style>
2.在application主题theme 或者 activity主题theme设置
android:theme="@style/TranslucentTheme"
3.设置状态栏字体图标颜色
在activity的onCreate()中setContentView之前设置
//状态栏中的文字颜色和图标颜色,需要android系统6.0以上,而且目前只有一种可以修改(一种是深色,一种是浅色即白色) if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //修改为深色,因为我们把状态栏的背景色修改为主题色白色,默认的文字及图标颜色为白色,导致看不到了。 getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); }
二、代码中动态修改状态栏颜色以及字体颜色
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//先将状态栏调整为透明
window.setStatusBarColor(Color.WHITE);
}
三、修改系统中默认的状态栏颜色
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/form_titlebar_bg</item> <item name="colorPrimaryDark">@color/form_titlebar_bg</item> </style>