【问题标题】:primary dark color android under API 21API 21 下的主深色 android
【发布时间】:2014-10-18 20:46:25
【问题描述】:

我想给状态栏上色,以便在 Android 5.0 中更好看。我想设置三个颜色的primarydark,light 和accent。

但我的最低 API 是 15。有没有机会在 API lvl 21 下使用它?或者我是否必须使用 min 创建一个单独的应用程序。 sdk 21?

编辑: 现在我得到了我需要的一切,但统计栏颜色不会改变。

这是我的价值观-v21/styles.xml

    <?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppBaseTheme" parent="android:Theme.Material.Light">
            <!-- API 21 theme customizations can go here. -->
    <!-- Main theme colors -->
    <!--   your app branding color for the app bar -->
    <item name="android:colorPrimary">@color/primary</item>
    <!--   darker variant for the status bar and contextual app bars -->
    <item name="android:colorPrimaryDark">@color/primary_dark</item>
    <!--   theme UI controls like checkboxes and text fields -->
    <item name="android:colorAccent">@color/accent</item>
        </style>
</resources>

这是正常的 style.xml

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
        parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
        parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@color/blue</item>
    </style>
</resources>

任何想法为什么这不起作用?

【问题讨论】:

    标签: android android-5.0-lollipop


    【解决方案1】:

    您可以为不同的 API 级别设置不同的样式。

    对于 API res/values/styles.xml,然后对于 API 21+,您将使用 res/values-v21/styles.xml

    如果设备运行 API 21 或更高版本,则它将使用 -v21 文件夹中的文件。如果您只想设置 &lt;color&gt; 值,那么只需将键命名为相同并使用 colors.xml 执行相同操作。

    http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources

    例子:

    res/values/colors.xml:

    <!-- Colours for API <21 -->
    <color name="primaryDark">#800000</color>
    <color name="light">#800080</color>
    <color name="accent">#FF0000</color>
    

    res/values-v21/colors.xml:

    <!-- Colours for API 21+ -->
    <color name="primaryDark">#000008</color>
    <color name="light">#080008</color>
    <color name="accent">#0000FF</color>
    

    【讨论】:

    • 谢谢!找不到任何关于它的信息。
    • 添加了我的 XML 文件。它对我不起作用。仍然有黑色的状态栏。
    • 您是否也指定了不同的colors.xml 文件?
    • 刚刚创建了 Thema。我在哪里可以指定它们?
    • 您将在您的 styles.xml 文件中使用它们,就像您在问题中所使用的那样。确保它们的名称相同,因为我的示例和您的问题有不同的命名。
    【解决方案2】:

    尝试以编程方式设置背景:

    actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_background));
    
    
    actionbar_background.xml
    
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="rectangle">
                <solid android:color="#e59300" />
            </shape>
        </item>
        <item android:bottom="3dp">
            <shape>
                <solid android:color="#fbb903" />
            </shape>
        </item>
    </layer-list>
    

    【讨论】:

      【解决方案3】:

      colorPrimaryDark 用于为状态栏着色...状态栏主题是在 Lollipop 中添加到 Android 的一项功能。 请记住,旧设备上的状态栏将是黑色的(无论主题指定什么)。

      参考自《Android编程指南2ed》大书呆子牧场指南P360。 但我在 android 开发者指南中找不到它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-25
        • 2015-05-28
        • 1970-01-01
        • 2022-06-10
        • 1970-01-01
        • 2019-12-20
        相关资源
        最近更新 更多