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

我一直在学习Android,到目前为止,我一直专注于function,但现在我开始使用布局。

我想知道什么是维护字体和颜色样式的最佳做法。 我做了一个colors.xml文件,我用它来改变button等单独元素的颜色,但我不确定Android是如何让开发人员组织它们的样式的。

例如,我希望所有屏幕都具有相同的背景颜色。 我怎么做? 是我需要为每个活动布局xml指定的东西吗? 或其他地方? 我如何完成它?

谢谢!

确保每个活动都具有相同背景颜色的快速而简单的方法是为您的活动创build一个主题。 该主题将指定android:windowBackground。

首先在values / colors.xml中定义颜色

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

在引用该颜色的res / values中创build一个themes.xml文件:

 <resources> <style name="MyTheme" parent="@android:style/Theme.Light"> <item name="android:windowBackground">@color/background</item> </style> </resources> 

…然后在你的AndroidManifest.xml中指定这个作为你的活动使用的主题。

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

更新你的Android Studio到1.4它有内置的主题编辑器。 如下图所示

在这里输入图像说明

您也可以在AndroidManifest.xml的应用程序级别设置背景

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

(注:由于我的声望太低,我不能将此作为对已接受答案的评论)。