状态栏颜色 - Android Studio

Status Bar Color - Android Studio

如何更改我在 Android Studio 中制作的应用的状态栏颜色,以便它在 Android Lollipop 5.0 或更高版本上变为静态颜色,并且在 运行 较低版本的 Android OS 上不会崩溃。

尝试使用这个,这对我有用

//changing statusbar
if (android.os.Build.VERSION.SDK_INT >= 21){
                Window window = this.getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.setStatusBarColor(this.getResources().getColor(R.color.primary_dark));
            }

改变颜色 ActionBar :

 <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">#ffff00</item>
    </style>

并在应用程序标签中声明您的清单

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/MyActionBar" >

希望对您有所帮助

您可以通过两种方式做到这一点,或者向您的主题添加属性,例如

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">>#ED3B3B</item>

    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">#BE2F2F</item>

    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <item name="colorAccent">#4DB6AC</item>

    <!-- You can also set colorControlNormal, colorControlActivated
         colorControlHighlight & colorSwitchThumbNormal. -->
</style>

并且您的 AndroidManifest 的应用程序标签必须具有带有 AppTheme 的主题属性

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"> // This

或者,如果您希望稍后动态更改状态栏颜色,例如单击按钮或图像加载完成后,您可以在 Java class

if (android.os.Build.VERSION.SDK_INT >= 21){
    Window window = activity.getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    window.setStatusBarColor("your color"));
}

请将此添加到您的应用样式中

<item name="colorPrimary">>#FFFF00</item>

<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#000000</item>