如何更改 StatusBar 颜色 android studio?

How to change StatusBar colour android studio?

无论我在主题中尝试什么,我似乎都无法将状态栏的颜色从这种灰色更改为:

这是我的主题文件:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.AlarmClockProject" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/teal_200</item>
        <item name="colorPrimaryVariant">@color/white</item>
        <item name="colorOnPrimary">@color/black</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:windowLightStatusBar">true</item>
        <item name="android:statusBarColor">#FF000000</item>
        <!-- Customize your theme here. -->
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>
</resources>

我已经尝试了多种变体,删除了其中一些等等。

我试过更改 colorPrimary/variant 等无济于事。

表示使用 values-v23/styles.xml 但这似乎过时了,因为我没有 styles.xml,现在它是主题,而且这一行无论如何都包含在上面。

target/compileSDK为32,minSDK为23

    res-> values-> themes.xml
    
     <style name="Theme.AlarmClockProject" parent="Theme.MaterialComponents.Light.NoActionBar">
            <!-- Primary brand color. -->
            <item name="colorPrimary">@color/black</item>
            <item name="colorPrimaryVariant">@color/white</item>
            <item name="colorOnPrimary">@color/white</item>
            <!-- Secondary brand color. -->
            <item name="colorSecondary">@color/black</item>
            <item name="colorSecondaryVariant">@color/teal_700</item>
            <item name="colorOnSecondary">@color/black</item>
            <!-- Status bar color. -->
            <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
            <item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
    
            <item name="windowActionBar">false</item>
            <item name="windowNoTitle">true</item>
            <item name="colorControlHighlight">@color/green_color</item>
          
        </style>


colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="purple_200">#FFBB86FC</color>
    <color name="purple_500">#FF6200EE</color>
    <color name="purple_700">#FF3700B3</color>
    <color name="teal_200">#FF03DAC5</color>
    <color name="teal_700">#FF018786</color>
    <color name="green_200">#57CC75</color>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
</resources>

将您的自定义颜色添加到 themes.xml 并且不要忘记添加到 night\themes.xml 文件中。否则,您的状态栏颜色将显示 DarkMode 中的默认颜色。

path-1:- res>values>themes>themes.xml

path-2:- res>values>themes>night\themes.xml

将您的自定义颜色添加到 primaryVariant

<item name="colorPrimaryVariant">@color/my_color</item>

然后在 colors.xml

中添加以下行

路径:- res>值>colors.xml

<color name="my_color">#888888</color>

此处此代码是 #888888 灰色。您可以在此处添加自定义颜色代码。

EDIT-1

设置Statusbar颜色Programmatically

将以下代码添加到 onCreate 方法中的 activity

        Window window = this.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(ContextCompat.getColor(this,R.color.black)); // here is your color

希望对您有所帮助。