【问题标题】:How to change status bar color to match app in Lollipop? [Android]如何更改状态栏颜色以匹配 Lollipop 中的应用程序? [安卓]
【发布时间】:2015-01-21 11:36:12
【问题描述】:

在新的棒棒糖更新中,我注意到对于原生 Google 应用,状态栏的颜色会发生变化,以匹配您正在运行的应用上的操作栏。我在 Twitter 应用程序上也看到了它,所以我猜不仅仅是谷歌可以做到这一点。

如果可能的话,有谁知道如何做到这一点?

【问题讨论】:

    标签: android colors status android-5.0-lollipop


    【解决方案1】:

    要更改状态栏颜色,请使用setStatusBarColor(int color)。 根据 javadoc,我们还需要在窗口上设置一些标志。

    工作的sn-p代码:

    Window window = activity.getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    window.setStatusBarColor(ContextCompat.getColor(activity, R.color.example_color));
    


    请记住according Material Design guidelines 状态栏颜色和操作栏颜色应该不同:

    • ActionBar 应该使用 primary 500 color
    • StatusBar 应该使用 primary 700 color

    看下面的截图:

    【讨论】:

    • 请注意,效果可能不会出现在模拟器中。例如,this sample project 在 Nexus 9 上为状态栏着色,但在 Android 5.0 模拟器上则不着色。
    • @mate:我指的是 Android SDK 模拟器。很高兴知道它正在开发 Genymotion。
    • @Azad 不,它没有。要使用上面的 sn-p 代码,您必须确保您的设备在 API 21 或更高版本上运行。
    • 我正在做一个相对简单的项目,我必须说我刚刚使用了getWindow().setStatusBarColor(activity.getResources().getColor(R.color.example_color));,而且效果很好。不确定标志是绝对必要的上下文。
    • 我也想在棒棒糖之前的设备中显示颜色。怎么办?
    【解决方案2】:

    只需将其添加到您的styles.xml 中。 colorPrimary 用于操作栏,colorPrimaryDark 用于状态栏。

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:colorPrimary">@color/primary</item>
        <item name="android:colorPrimaryDark">@color/primary_dark</item>
    </style>
    

    来自开发者 android 的这张图片解释了有关调色板的更多信息。你可以阅读更多关于link的内容。

    【讨论】:

    • Android Studio 为每个项目创建以下条目:&lt;color name="colorPrimary"&gt;#somecolor&lt;/color&gt;&lt;color name="colorPrimaryDark"&gt;#somecolor&lt;/color&gt;。可以更改它们以达到预期的效果。
    【解决方案3】:

    另一种设置状态栏颜色的方法是通过style.xml

    为此,请在 res/values-v21 文件夹下创建一个 style.xml 文件,其中包含以下内容:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="AppTheme" parent="android:Theme.Material">
            <!--   darker variant for the status bar and contextual app bars -->
            <item name="android:colorPrimaryDark">@color/blue_dark</item>
        </style>
    </resources>
    

    编辑: 正如 cmets 中所指出的,当使用 AppCompat 时,代码是不同的。在文件 res/values/style.xml 中使用:

    <style name="Theme.MyTheme" parent="Theme.AppCompat.Light">   
        <!-- Set AppCompat’s color theming attrs -->
        <item name="colorPrimary">@color/my_awesome_red</item>
        <item name="colorPrimaryDark">@color/my_awesome_darker_red</item>
        <!-- Other attributes -->
    </style>
    

    【讨论】:

    • 做到了。但没有奏效。但后来我使用 AppCompat 而不是 Material。
    • @Martin 对于 appcompat,请使用不带 android 前缀的 colorPrimaryDark。
    • 不适用于 appcompat(在 18 级测试)
    • 状态栏颜色仅适用于 OS API 级别 21 及以上的设备。此外,在您的清单中确保您没有在个别活动上覆盖此样式。
    • 对于inappbrowser如何更改搜索栏颜色与应用颜色相同
    【解决方案4】:

    要设置状态栏颜色,在 res/values-v21 文件夹下创建一个 style.xml 文件,内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <style name="AppBaseTheme" parent="AppTheme">
            <item name="android:windowDrawsSystemBarBackgrounds">true</item>
            <item name="android:statusBarColor">@color/blue</item>
        </style>
    
    </resources>
    

    【讨论】:

    • 它说无法解析符号 windowDrawsSystemBarBackgrounds
    【解决方案5】:

    如果您使用两种样式,请在 v21 样式中添加此行。

      <item name="android:statusBarColor">#43434f</item>
    

    【讨论】:

    • 如果不设置标志android:windowDrawsSystemBarBackgrounds,这将不起作用
    【解决方案6】:

    此外,如果您希望为不同的活动 (fragments) 使用不同的 status-bar 颜色,您可以通过以下步骤来实现(使用 API 21 及更高版本):

    首先创建values21/style.xml并输入以下代码:

     <style name="AIO" parent="AIOBase">
                <item name="android:windowDrawsSystemBarBackgrounds">true</item>
                <item name="android:windowContentTransitions">true</item>
        </style>
    

    然后在您的 values/style.xml 中定义 White|Dark 主题,如下所示:

     <style name="AIOBase" parent="Theme.AppCompat.Light.NoActionBar">
            <item name="colorPrimary">@color/color_primary</item>
            <item name="colorPrimaryDark">@color/color_primary_dark</item>
            <item name="colorAccent">@color/color_accent</item>
            <item name="android:textColorPrimary">@android:color/black</item>
            <item name="android:statusBarColor" tools:targetApi="lollipop">@color/color_primary_dark
            </item>
            <item name="android:textColor">@color/gray_darkest</item>
            <item name="android:windowBackground">@color/default_bg</item>
            <item name="android:colorBackground">@color/default_bg</item>
        </style>
    
    
        <style name="AIO" parent="AIOBase" />
    
        <style name="AIO.Dark" parent="AIOBase">
            <item name="android:statusBarColor" tools:targetApi="lollipop">#171717
            </item>
        </style>
    
        <style name="AIO.White" parent="AIOBase">
            <item name="android:statusBarColor" tools:targetApi="lollipop">#bdbdbd
            </item>
        </style>
    

    也不要忘记在您的manifest.xml 中应用主题。

    【讨论】:

      【解决方案7】:

      在 android pre Lollipop 设备中,您可以通过 SystemBarTintManager 进行操作 如果您使用的是 android studio,只需在您的 gradle 文件中添加 Systembartint lib。

      dependencies {
          compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
          ...
      }
      

      然后在你的活动中

      // create manager instance after the content view is set
      SystemBarTintManager mTintManager = new SystemBarTintManager(this);
      // enable status bar tint
      mTintManager.setStatusBarTintEnabled(true);
      mTintManager.setTintColor(getResources().getColor(R.color.blue));
      

      【讨论】:

        猜你喜欢
        • 2015-02-12
        • 1970-01-01
        • 1970-01-01
        • 2013-03-20
        • 2020-07-24
        • 2017-01-23
        • 2022-11-13
        • 2016-04-28
        • 1970-01-01
        相关资源
        最近更新 更多