【问题标题】:Status Bar Color - Android Studio状态栏颜色 - Android Studio
【发布时间】:2015-10-08 02:49:01
【问题描述】:

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

【问题讨论】:

  • 嘿!我还发布了详细的答案..随时查看!

标签: android android-5.0-lollipop android-statusbar


【解决方案1】:

试试这个,这对我有用

//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" >

希望对你有帮助

【讨论】:

  • 在你想要显示状态栏颜色的活动中,只需在方法 onCreate 中复制粘贴
  • 这会自动更改颜色以匹配操作栏颜色,还是我必须更改特定值才能为其赋予颜色? (对不起,我是初学者)
  • 是的,您必须在 color.xml 中声明,例如 &lt;color name="primary_dark"&gt;#3f41ac&lt;/color&gt; 您可以通过更改该 hexa 来更改颜色匹配操作栏
  • 非常感谢...既然您在这里回答,我将顺其自然,再问您一个问题。我现在如何更改操作栏颜色? (我猜应该叫action bar吧?状态栏下面的那个吧?)
【解决方案2】:

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

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 类中使用

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"));
}

【讨论】:

  • 该应用在低于 5.0 的 Android 版本上运行不会崩溃吧?
  • 不会的。只是补充一下,因为你是新人。也尝试使用Toolbar。您可以用工具栏替换操作栏,它可以让您更好地控制您的操作栏。chris.banes.me/2014/10/17/appcompat-v21
【解决方案3】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-20
    • 2017-08-04
    • 2023-03-12
    • 2016-12-25
    • 2017-03-05
    • 2018-03-01
    • 2015-07-16
    相关资源
    最近更新 更多