修改状态栏颜色和状态栏字体颜色

一个activity多个fragment,修改每个fragment的状态栏颜色和状态栏字体的颜色
1、设置activity的状态栏透明,设置activity的theme
 value-v19.xml
 <style name="FirstPageStyle" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">true</item>
    </style>

 value-v21.xml
  <style name="FirstPageStyle" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>

2、设置fragment布局文件的根元素
 android:background="#176ce0"
    android:fitsSystemWindows="true"
    android:clipToPadding="true"
    android:paddingTop="20dp" 
//这个暂时写死20dp,实际上应该是状态栏的高度,可以在代码里面获取状态栏高度,然后在这个布局加个高度为状态栏高度的view占位

3、在activity的ViewPager的OnPageChangeListener里面的onPageSelected()函数设置状态栏字体颜色(以下方法只适用于6.0及以上系统)
  /**
* Flag只有在使用了FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
* 并且没有使用 FLAG_TRANSLUCENT_STATUS的时候才有效,也就是只有在状态栏全透明的时候才有效。
*
* @param activity
* @param bDark  bDark为true时是黑色的字,为false时是白色的字
*/
public static void setStatusBarMode(Activity activity, boolean bDark) {
//6.0以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
View decorView = activity.getWindow().getDecorView();
if (decorView != null) {
int vis = decorView.getSystemUiVisibility();
if (bDark) {
vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
} else {
vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}
decorView.setSystemUiVisibility(vis);
}
}
}

猜你喜欢

转载自blog.csdn.net/u010015933/article/details/81034645
今日推荐