如何在安卓应用中更改背景颜色

271

我希望能够以最简单的方式在我的安卓应用中将背景颜色更改为白色。

20个回答

371

你需要使用 android:background 属性,例如:

android:background="@color/white"

你还需要在strings.xml文件中添加白色的值。

<color name="white">#FFFFFF</color>

编辑:2012年11月18日

8位颜色代码的前两个字母提供alpha值,如果您使用html 6位颜色表示法,则颜色是不透明的。

例如:

输入图像描述


@Jonny,前两个字母提供了alpha值。 - Ravi Vyas
谢谢,但是使用十六进制值不是更容易吗? - the_prole
这很容易,但是不好的实践 :) - Ravi Vyas
4
使用 android:background="@android:color/white" 更加方便,这是预定义的,并且不需要您在 strings.xml 中添加任何内容。 - Greg Hewgill
1
你能否将位置/res/layout/activity_main.xml添加到答案中,以添加元素android:background - 030
只是为了澄清一下。给顶级布局元素设置边距将始终在边距中显示黑色。这个答案展示了如何设置屏幕本身的背景 https://dev59.com/BWkw5IYBdhLWcg3wE2dA - Martin Westin

164
你也可以使用。
android:background="#ffffff"

你可以在你的xml布局文件或/res/layout/activity_main.xml中更改主题,或者通过在AndroidManifest.xml中添加来更改主题

android:theme="@android:style/Theme.Light"

将此标签添加到您的活动标记中。

如果您想实现动态更改背景,请使用

YourView.setBackgroundColor(Color.argb(255, 255, 255, 255));

这是正确的答案,因为它有主题,我相信。一行代码并不是最简单的方法,但将整个风格应用到所有内容中是一种方式。样式是您实现即使是复杂更改也能够使用一行代码的方法。此外,样式基于偏好工作,而不像通用属性那样(尽管仍有一些奇怪的地方)。 - Stephen J

63

最简单的方式

android:background="@android:color/white"

无需定义任何东西。它使用android.R中预定义的颜色。


7
以编程方式: context.getResources().getColor(android.R.color.white)的意思是获取Android资源中白色颜色的值。 - Paschalis
1
谢谢,那正是我要找的。在我的情况下,我必须用Java来完成它。 - Armfoot

10

最简单的以编程方式更改背景颜色的方法(仅限代码 - 不涉及XML更改):

LinearLayout bgElement = (LinearLayout) findViewById(R.id.container);
bgElement.setBackgroundColor(Color.WHITE);

唯一要求是在 activity_whatever.xml 中的 "base" 元素必须有一个 id,你可以在 Java 中引用它(在此示例中为 container):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/container"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
     ...
</LinearLayout>

Paschalis和James在此回复后,帮助我找到了这个解决方案。他们在查看如何在代码中设置TextView文本颜色的各种可能性后给了我灵感。

希望能帮到有需要的人!


4
这个方法对我有用:
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.layout.rootLayout);
relativeLayout.setBackgroundColor(getResources().getColor(R.color.bg_color_2));

在布局xml中设置id

xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootLayout"
android:background="@color/background_color"

添加颜色值/color.xml

<color name="bg_color_2">#ffeef7f0</color>

4
最简单的方法是在layout.xml或/res/layout/activity_main.xml文件中添加android:background="#FFFFFF"到主节点:
<?xml version="1.0" encoding="utf-8"?>
   <TextView xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:padding="10dp"
       android:textSize="20sp" 
       android:background="#FFFFFF">
   </TextView>

这不是一个好主意,因为它会导致不必要的GPU过度绘制(首先,窗口背景将被绘制,然后是TextView的背景在其上方)。请参阅:http://www.curious-creature.com/docs/android-performance-case-study-1.html - Miloš Černilovský

4
使用渐变背景是最好的方式,因为这样不会增加应用程序的大小。对于安卓应用来说,图像是有害的,所以尽量少使用它们。与其使用单一颜色作为背景,不如在一个背景中使用多种颜色。 enter image description here enter image description here enter image description hereenter image description here

3
您可以在xml表中尝试此操作:
android:background="@color/background_color"

2
我希望您能够在最简单的方式下更改我的安卓应用程序的背景颜色为白色。
问题要求提供最简单的方法,以下是具体步骤:
在所有父视图中设置“parentViewStyle”,例如您的活动、片段和对话框的大多数父视图。
<LinearLayout style="@style/parentViewStyle">

  ... other components inside

</LinearLayout>

只需将此样式放入res>values>styles.xml中。
<style name="parentViewStyle">
    <item name="android:layout_height">match_parent</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:background">@color/white</item> // set your color here.
    <item name="android:orientation">vertical</item>
</style>

通过这种方式,你将来不必多次更改背景颜色。

2

在布局中,要更改背景,请使用以下内容。

android:background="@color/your_color"

在程序中可以使用以下方法。例如:TextView的背景色。
 TextView tvName = (TextView) findViewById(R.id.tvName);
 tvName.setBackgroundColor(getResources().getColor(R.color.your_color));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接