设置 Android 主题背景色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11779679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting Android Theme background color
提问by Stark
I'm trying to modify the default background theme color, which should be easy but surprisingly I can't get it working. Please note that I want the change to be across the entire app, not just for a single activity. Here is my code:
我正在尝试修改默认的背景主题颜色,这应该很容易,但令人惊讶的是我无法让它工作。请注意,我希望对整个应用程序进行更改,而不仅仅是针对单个活动。这是我的代码:
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>
and of course in the manifest
当然在清单中
<application
.
.
.
android:theme="@style/AppTheme" >
</application>
Android doc which I consulted on modifying themes: http://developer.android.com/guide/topics/ui/themes.html
我在修改主题时咨询的 Android 文档:http: //developer.android.com/guide/topics/ui/themes.html
I've tried switching between white_opaque and pitch_black for all the xml attributes but it doesn't change a thing. Any suggestions?
我已经尝试在所有 xml 属性的 white_opaque 和 pitch_black 之间切换,但它没有改变任何事情。有什么建议?
采纳答案by Stark
Okay turned out that I made a really silly mistake. The device I am using for testing is running Android 4.0.4, API level 15.
好吧,事实证明我犯了一个非常愚蠢的错误。我用于测试的设备运行的是 Android 4.0.4,API 级别 15。
The styles.xml file that I was editing is in the default values folder. I edited the styles.xml in values-v14 folder and it works all fine now.
我正在编辑的styles.xml 文件位于默认值文件夹中。我编辑了 values-v14 文件夹中的styles.xml,现在一切正常。
回答by Oded Breiner
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.NoActionBar">
<item name="android:windowBackground">@android:color/black</item>
</style>
</resources>
回答by Blasanka
Open res -> values -> styles.xml
and to your <style>
add this line replacing with your image path <item name="android:windowBackground">@drawable/background</item>
. Example:
打开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>
There is a <item name ="android:colorBackground">@color/black</item>
also, that will affect not only your main window background but all the component in your app. Read about customize theme here.
还有一个<item name ="android:colorBackground">@color/black</item>
,它不仅会影响您的主窗口背景,还会影响您应用程序中的所有组件。在此处阅读自定义主题。
If you want version specific styles:
如果你想要版本特定的样式:
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.
如果新版本的 Android 添加了您想要使用的主题属性,您可以将它们添加到您的主题中,同时仍然与旧版本兼容。您所需要的只是保存在包含资源版本限定符的 values 目录中的另一个 styles.xml 文件。例如:
res/values/styles.xml # themes for all versions res/values-v21/styles.xml # themes for API level 21+ only
由于 values/styles.xml 文件中的样式适用于所有版本,因此 values-v21/styles.xml 中的主题可以继承它们。因此,您可以通过从“基本”主题开始,然后将其扩展为特定于版本的样式来避免重复样式。