Android-如何设置所有屏幕的背景颜色?

保持字体和颜色样式的最佳实践是什么。我创建了一个 Colors.xml 文件,我用它来改变单独元素(如按钮)的颜色,但我不确定 Android 希望开发人员如何组织他们的样式。

例如,我希望所有屏幕具有相同的背景颜色。我该怎么做?是否需要为每个 Activity 布局 xml 指定它?或者其他地方?我该怎么做?

104151 次浏览

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.

First define the color in values/colors.xml

<resources>
<color name="background">#FF0000 </color>
</resources>

Create a themes.xml file in res/values that references that color:

<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.

 <activity
android:name=".MyActivity"
android:theme="@style/MyTheme" />

You can also set the background at the application level in AndroidManifest.xml

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

(Note: I cannot this as a comment of the accepted answer as my reputation is too low).

Update your android studio to 1.4 it has inbuilt theme editor. as you can see in below image

enter image description here