Android - 如何设置所有屏幕的背景颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10156816/
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
Android - how to set background color of all screens?
提问by GeekedOut
What is the best practice to maintain styles of fonts and colors. I made a colors.xml file which I have used to change colors on seperate elements like buttons, but I am not sure how Android wants developers to organize their styles.
保持字体和颜色样式的最佳做法是什么。我制作了一个 color.xml 文件,我用它来更改按钮等单独元素的颜色,但我不确定 Android 希望开发人员如何组织他们的样式。
For example, I would like all screens to have the same background color. How do I do that? Is it something I need to specify for each Activity layout xml? Or elsewhere? How do I accomplish it?
例如,我希望所有屏幕都具有相同的背景颜色。我怎么做?我需要为每个活动布局 xml 指定什么吗?还是别处?我该如何实现?
回答by NPike
A quick and easy way to make sure every activity has the same background color, is to create a theme for your activities to use. That theme would specify the android:windowBackground.
确保每个活动具有相同背景颜色的一种快速简便的方法是为您的活动创建一个主题以供使用。该主题将指定 android:windowBackground。
First define the color in values/colors.xml
首先在 values/colors.xml 中定义颜色
<resources>
<color name="background">#FF0000 </color>
</resources>
Create a themes.xml file in res/values that references that color:
在引用该颜色的 res/values 中创建一个 themes.xml 文件:
<resources>
<style name="MyTheme" parent="@android:style/Theme.Light">
<item name="android:windowBackground">@color/background</item>
</style>
</resources>
... and then in your AndroidManifest.xml specify this as the theme for your activities to use.
...然后在您的 AndroidManifest.xml 中将其指定为您要使用的活动的主题。
<activity
android:name=".MyActivity"
android:theme="@style/MyTheme" />
回答by kundan roy
回答by chris
You can also set the background at the application level in AndroidManifest.xml
也可以在AndroidManifest.xml中设置应用层的背景
<application
...
android:theme="@style/MyTheme">
(Note: I cannot this as a comment of the accepted answer as my reputation is too low).
(注意:我不能将此作为对已接受答案的评论,因为我的声誉太低)。