android 状态栏和虚拟按键栏字体背景颜色设置

Posted 踏雪羽翼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 状态栏和虚拟按键栏字体背景颜色设置相关的知识,希望对你有一定的参考价值。

 

1、设置顶部状态栏字体颜色为白色红底

public static void setStatusBarColor(Activity context, boolean useThemeStatusBarColor) 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) //5.0及以上
            View decorView = context.getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_VISIBLE;
            decorView.setSystemUiVisibility(option);

            //根据上面设置是否对状态栏单独设置颜色
            if (useThemeStatusBarColor) 
                context.getWindow().setStatusBarColor(Color.parseColor("#FF3E96"));
             else 
                context.getWindow().setStatusBarColor(Color.TRANSPARENT);
            
         else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) //4.4到5.0
            WindowManager.LayoutParams localLayoutParams = context.getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        
    

2、设置状态栏字体颜色为黑色

 public static void setStatusBarColor(Activity context, boolean useThemeStatusBarColor) 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
            View decorView = context.getWindow().getDecorView();

             context.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
            
            if (useThemeStatusBarColor) 
                context.getWindow().setStatusBarColor(Color.parseColor("#FF3E96"));
                context.getWindow().setNavigationBarColor(Color.parseColor("#FF3E96"));//40000000
             else 
                context.getWindow().setStatusBarColor(Color.TRANSPARENT);
            
         else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) //4.4到5.0
            WindowManager.LayoutParams localLayoutParams = context.getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        
    

3、设置虚拟按键栏背景颜色

public static void setNavigationBarColor(Activity activity)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
            activity.getWindow().setNavigationBarColor(Color.parseColor("#FF3E96"));
        

    

4、设置虚拟按键栏和状态栏字体图标为黑色,白底

public static void setStatusBarColor(Activity context, boolean useThemeStatusBarColor) 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) //5.0及以上
context.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);

             //根据上面设置是否对状态栏单独设置颜色
            if (useThemeStatusBarColor) 
                context.getWindow().setStatusBarColor(Color.parseColor("#ffffffff"));
                context.getWindow().setNavigationBarColor(Color.parseColor("#ffffffff"));//40000000
             else 
                context.getWindow().setStatusBarColor(Color.TRANSPARENT);
            
         else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) //4.4到5.0
            WindowManager.LayoutParams localLayoutParams = context.getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        
    

这些方法不一定适配所有手机,如果是DialogFragment就将activity改成Dialog即可。

 

 

以上是关于android 状态栏和虚拟按键栏字体背景颜色设置的主要内容,如果未能解决你的问题,请参考以下文章

Android 去除状态栏和隐藏虚拟按键

Android设置状态栏颜色和状态栏文字、图标颜色

Android标题栏状态栏图标文字颜色及背景动态变化

根据状态栏颜色亮度设置黑白字体(Android 6.0及以上版本)

android导航栏与状态栏颜色及透明度

如何在android中将状态栏背景设置为渐变色或drawable