设置statusbar的颜色,适用于SDK4.4版本及以上版本

本文介绍如何在Android应用中自定义状态栏的颜色,并提供了一种简单的方法来实现这一功能。适用于不同版本的Android系统。

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

Android应用,自定义statusbar的颜色

1、效果图

这里写图片描述

2、方法

为了方便调用,你可以将此方法封装在BaseActivity中。

protected void setStatusBarBackground() {
        int color = Color.BLUE;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            color = getResources().getColor(R.color.colorPrimary, getTheme());
        } else {
            color = getResources().getColor(R.color.colorPrimary);
        }
        setStatusBarBackground(color);
    }

    /**
     * <p>设置statusBar的背景颜色。
     * <br>在setContentView()方法之后调用。
     * <br>SDK4.4以下版本无效。
     * @param background
     */
    protected void setStatusBarBackground(int background) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            }
            //设置statusBar的颜色
            getWindow().setStatusBarColor(background);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            // 1.设置状态栏透明,经测试在代码里直接声明透明状态栏更有效
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content);
            View statusBarView = contentView.getChildAt(0);
            //改变颜色时避免重复添加statusBarView
            int statusbarHeight = getStatusBarHeight();
            if (statusBarView == null || statusBarView.getMeasuredHeight() != statusbarHeight) {
                statusBarView = new View(this);
            }
            statusBarView.setBackgroundColor(background);
            contentView.addView(statusBarView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusbarHeight));
        }
    }
3、使用

values文件夹下的styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar"/>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resourcs>

values-v19文件夹下的styles.xml

<resources>
    <style name="AppTheme.NoActionBar">
        <item name="android:windowTranslucentStatus">true</item>
    </style>

</resources>

values-v21文件夹下的styles.xml

 <resources>

    <style name="AppTheme.NoActionBar">
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
</resources>
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input_password);
        setStatusBarBackground();

        //do your job
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值