如何在安卓系统中改变状态栏的颜色?

565

3
只有在KitKat之后才有可能实现。@MarkBuikema - codercat
9
如Niels所建议,从v21开始有一个名为 android:statusBarColor 的主题属性。你可以简单地将其添加到 values-v21/styles.xml 中。 - Levite
@MarkBuikema 这跟root访问权限有什么关系! - Steve Moretz
请查看此答案(使状态栏为白色,图标为黑色)。链接:https://dev59.com/gV4c5IYBdhLWcg3wuMJ7#74375368 - Rumit Patel
32个回答

16

要更改状态栏的颜色,请前往res/values-v21/styles.xml并更改状态栏的颜色。

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@color/color_primary</item>
        <item name="colorPrimaryDark">@color/color_secondary</item>
        <item name="colorAccent">@color/color_accent</item>
        <item name="android:statusBarColor">#0000FF</item>
    </style>
</resources>

嘿,你能帮我吗?我对Android一无所知,现在也无法开始学习。我有一个简单的应用程序,可以运行一个HTML文件。我非常想改变状态栏的颜色,因为黑色的顶部和底部对我的调色板来说太可怕了。但是我找不到styles.xml文件,甚至连它都找不到...如果我可以把我的apk发送给你,让你帮我设置一下,我将非常感激! - Thiago Soubra

15

我有这个需求:通过编程改变状态栏颜色并保持其透明,以允许导航抽屉在其上绘制自身,覆盖透明状态栏。

我无法使用API实现此功能。

getWindow().setStatusBarColor(ContextCompat.getColor(activity ,R.color.my_statusbar_color)

如果您在Stack Overflow中查看此行代码之前的内容,每个人都将状态栏的透明度设置为不透明。

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)

我可以像这样管理状态栏的颜色和透明度:

  • 在Android 4上,由于您无法从API中管理状态栏颜色,所以您无法做太多事情...您唯一能做的是将状态栏设置为半透明,并将UI的有色元素移动到状态栏下面。要做到这一点,您需要调整:

android:fitsSystemWindows="false"

在你的主布局中添加一个属性,使得你可以在状态栏下绘制布局。然后你需要在主布局顶部进行一些padding的调整。

  • Android 5及以上版本:你需要定义一个样式,包含

  • <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    

    这使得导航抽屉可以覆盖状态栏。

    然后要更改颜色并保持状态栏透明,您需要使用以下设置状态栏颜色:

    drawerLayout.setStatusBarBackgroundColor(ContextCompat.getColor(activity, R.color.my_statusbar_color))
    

    其中drawerLayout的定义如下

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
    

    没有使用 <item name="android:windowDrawsSystemBarBackgrounds">true</item> 也可以在我的设备上运行。无论如何,在经过数天的搜索后,我终于找到了一些真正有效的方法(目前仅在Lollipop上测试)。谢谢。 - DenisGL

    11

    如果您想以编程方式更改状态栏颜色(并且设备已经安装了Android 5.0),那么这是一种从任何活动更改 statusBarColor 的简单方法,并且在不同的片段具有不同状态栏颜色时非常容易实现。

     /**
     * @param colorId id of color
     * @param isStatusBarFontDark Light or Dark color
     */
    fun updateStatusBarColor(@ColorRes colorId: Int, isStatusBarFontDark: Boolean = true) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val window = window
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window.statusBarColor = ContextCompat.getColor(this, colorId)
            setSystemBarTheme(isStatusBarFontDark)
        }
    }
    
    /** Changes the System Bar Theme.  */
    @RequiresApi(api = Build.VERSION_CODES.M)
    private fun setSystemBarTheme(isStatusBarFontDark: Boolean) {
        // Fetch the current flags.
        val lFlags = window.decorView.systemUiVisibility
        // Update the SystemUiVisibility depending on whether we want a Light or Dark theme.
        window.decorView.systemUiVisibility = if (isStatusBarFontDark) lFlags and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv() else lFlags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
    }
    

    7
    如果您想要在Android 4.4及以上版本上工作,请尝试以下方法。我参考了Harpreet的答案和这个链接:Android和透明状态栏
    首先,在Activity的onCreate方法中调用setStatusBarColored方法(我将其放在一个工具类中)。我在这里使用了一张图片,您可以将其更改为使用颜色。
    public static void setStatusBarColored(Activity context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            Window w = context.getWindow();
            w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            int statusBarHeight = getStatusBarHeight(context);
    
            View view = new View(context);
            view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            view.getLayoutParams().height = statusBarHeight;
            ((ViewGroup) w.getDecorView()).addView(view);
            view.setBackground(context.getResources().getDrawable(R.drawable.navibg));
        }
    }
    
    public static int getStatusBarHeight(Activity context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
    

    之前: before

    之后: after

    状态栏的颜色已经改变,但导航栏被裁掉了,所以我们需要在onCreate方法中设置导航栏的margin或offset。

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, (int)(this.getResources().getDimension(R.dimen.navibar_height)));
            layoutParams.setMargins(0, Utils.getStatusBarHeight(this), 0, 0);
    
            this.findViewById(R.id.linear_navi).setLayoutParams(layoutParams);
        }
    

    然后状态栏将会变成这样。 状态栏

    7

    编辑Values文件夹中的colors.xml,将colorPrimary更改为您想要的状态栏颜色。例如:

       <resources>
    <color name="colorPrimary">#800000</color> // changes the status bar color to Burgundy
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="red">#FF0000</color>
    <color name="white">#FFFFFF</color>
    <color name="cream">#fffdd0</color>
    <color name="burgundy">#800000</color>
    


    6

    只需将以下内容添加到styles.xml文件中即可

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- This is used for statusbar color. -->
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <!-- This is used for statusbar content color. If statusbarColor is light, use "true" otherwise use "false"-->
        <item name="android:windowLightStatusBar">false</item>
    </style>
    

    6
    解决方案非常简单,将以下行添加到您的style.xml文件中。
    对于深色模式:
    <item name="android:windowLightStatusBar">false</item>
    <item name="android:statusBarColor">@color/black</item>
    

    4

    这是我在KitKat中尝试过并且效果不错的方法。

    public static void setTaskBarColored(Activity context) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            {
                Window w = context.getWindow();
                w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                //status bar height
                int statusBarHeight = Utilities.getStatusBarHeight(context);
    
                View view = new View(context);
                view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                view.getLayoutParams().height = statusBarHeight;
                ((ViewGroup) w.getDecorView()).addView(view);
                view.setBackgroundColor(context.getResources().getColor(R.color.colorPrimaryTaskBar));
            }
        }
    

    4

    在res/values/styles.xml文件中,将colorPrimaryDark更改为所需的颜色。

        <resources>
            <color name="colorPrimary">#800000</color>
            <color name="colorPrimaryDark">#303F9F</color> //This Line
            <color name="colorAccent">#FF4081</color>
            <color name="red">#FF0000</color>
            <color name="white">#FFFFFF</color>
           <color name="cream">#fffdd0</color>
           <color name="burgundy">#800000</color>
        </resources>
    

    3

    Java: 在Activity的onCreate方法中使用此代码。

    Window window = this.getWindow();
    window.setStatusBarColor(this.getResources().getColor(R.color.main_screen_bg_color));
    

    Kotlin:

    window.statusBarColor = ContextCompat.getColor(this, R.color.colorName)
    

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接