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

博客介绍了设置顶部状态栏字体颜色为白色红底、黑色,设置虚拟按键栏背景颜色,以及设置虚拟按键栏和状态栏字体图标为黑色、白底的方法,同时提醒这些方法不一定适配所有手机,若为DialogFragment将activity改成Dialog。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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即可。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

踏雪羽翼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值