设置Android主题背景颜色

127

我试图修改默认的背景主题颜色,这本应该很容易,但出乎意料的是我无法让它工作。请注意,我希望更改适用于整个应用程序,而不仅仅是单个活动。这是我的代码:

styles.xml

<resources>

    <color name="white_opaque">#FFFFFFFF</color>
    <color name="pitch_black">#FF000000</color>

    <style name="AppTheme" parent="android:Theme.Light">
        <item name="android:background">@color/white_opaque</item>
        <item name="android:windowBackground">@color/white_opaque</item>
        <item name="android:colorBackground">@color/white_opaque</item>
    </style>

</resources>

当然,在清单文件中也要这样做。

<application
    .
    .
    .
    android:theme="@style/AppTheme" > 
</application>

我查阅了 Android 文档中关于修改主题的内容:http://developer.android.com/guide/topics/ui/themes.html

我已经尝试将所有 XML 属性从 white_opaque 切换到 pitch_black,但没有任何变化。有什么建议吗?


1
尝试移除 Alpha 通道。 - Blundell
好的,我刚刚做了,但还是一样。 - Stark
1
这是我读过的最好的解决方案。 https://dev59.com/BWkw5IYBdhLWcg3wE2dA#33213433 - Ata Iravani
3个回答

62

好的,事实证明我犯了一个非常愚蠢的错误。我用来测试的设备运行的是Android 4.0.4,API级别为15。

我编辑的styles.xml文件在默认值文件夹中。我编辑了values-v14文件夹中的styles.xml文件,现在一切都正常了。


4
如果您没有使用其他样式文件夹,可以直接删除它们。我曾经遇到类似的问题,只需在“values”文件夹中使用一个styles.xml文件即可解决。我的问题是在清单文件中将Activity设置为主题项(这当然会覆盖AppTheme)。 - Stephen Hosking

57
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Holo.NoActionBar">
        <item name="android:windowBackground">@android:color/black</item>
    </style>

</resources>

19

打开res->values->styles.xml 文件,然后在你的<style>中添加这一行,将其中的图片路径替换为你自己的图片路径:<item name="android:windowBackground">@drawable/background</item>。 例如:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowBackground">@drawable/background</item>
    </style>

</resources>

有一个<item name="android:colorBackground">@color/black</item>,它将影响您应用程序中所有组件的主窗口背景。了解如何自定义主题,请在此处阅读。

如果您想要版本特定的样式

If a new version of Android adds theme attributes that you want to use, you can add them to your theme while still being compatible with old versions. All you need is another styles.xml file saved in a values directory that includes the resource version qualifier. For example:

res/values/styles.xml        # themes for all versions
res/values-v21/styles.xml    # themes for API level 21+ only

Because the styles in the values/styles.xml file are available for all versions, your themes in values-v21/styles.xml can inherit them. As such, you can avoid duplicating styles by beginning with a "base" theme and then extending it in your version-specific styles.

在这里阅读更多(主题文档)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接