Android - 如何设置所有屏幕的背景颜色? [英] Android - how to set background color of all screens?

查看:36
本文介绍了Android - 如何设置所有屏幕的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

保持字体和颜色样式的最佳做法是什么.我制作了一个 color.xml 文件,我用它来更改按钮等单独元素的颜色,但我不确定 Android 希望开发人员如何组织他们的样式.

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.

例如,我希望所有屏幕都具有相同的背景颜色.我怎么做?我需要为每个活动布局 xml 指定什么吗?还是别处?我该如何实现?

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?

推荐答案

确保每个活动具有相同背景颜色的一种快速简便的方法是为您的活动创建一个主题以供使用.该主题将指定 android:windowBackground.

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.

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

First define the color in values/colors.xml

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

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

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>

... 然后在您的 AndroidManifest.xml 中将其指定为您要使用的活动的主题.

... and then in your AndroidManifest.xml specify this as the theme for your activities to use.

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

这篇关于Android - 如何设置所有屏幕的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆