如何在Android应用程序中更改背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2748830/
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
How to change background color in android app
提问by ryan
I want to be able to change the background color to white in my android app in the simplest way possible.
我希望能够以最简单的方式将我的 android 应用程序中的背景颜色更改为白色。
回答by Ravi Vyas
You need to use the android:background property , eg
您需要使用 android:background 属性,例如
android:background="@color/white"
Also you need to add a value for white in the strings.xml
您还需要在 strings.xml 中为 white 添加一个值
<color name="white">#FFFFFF</color>
Edit : 18th Nov 2012
编辑:2012 年 11 月 18 日
The first two letters of an 8 letter color code provide the alpha value, if you are using the html 6 letter color notation the color is opaque.
8 个字母颜色代码的前两个字母提供 alpha 值,如果您使用 html 6 个字母颜色表示法,颜色是不透明的。
Eg :
例如:
回答by James
You can also use
你也可以使用
android:background="#ffffff"
in your xml layout or /res/layout/activity_main.xml
, or you can change the theme in your AndroidManifest.xml by adding
在您的 xml 布局或/res/layout/activity_main.xml
,或者您可以通过添加更改 AndroidManifest.xml 中的主题
android:theme="@android:style/Theme.Light"
to your activity tag.
到您的活动标签。
If you want to change the background dynamically, use
如果要动态更改背景,请使用
YourView.setBackgroundColor(Color.argb(255, 255, 255, 255));
回答by Paschalis
Simplest way
最简单的方法
android:background="@android:color/white"
android:background="@android:color/white"
No need to define anything. It uses predefined colors in android.R
.
不需要定义任何东西。它在android.R
.
回答by Armfoot
To change the background color in the simplest way possible programmatically (exclusively - no XML changes):
要以最简单的方式以编程方式更改背景颜色(仅限 - 不更改 XML):
LinearLayout bgElement = (LinearLayout) findViewById(R.id.container);
bgElement.setBackgroundColor(Color.WHITE);
Only requirement is that your "base" element in the activity_whatever.xml has an id which you can reference in Java (container
in this case):
唯一的要求是您在 activity_whatever.xml 中的“基本”元素具有您可以在 Javacontainer
中引用的 ID(在本例中):
<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 and James, who replied here, kind of lead me to this solution, after checking out the various possibilities in How to set the text color of TextView in code?.
在这里回复的 Paschalis 和 James 在查看了如何在代码中设置 TextView 的文本颜色中的各种可能性之后,有点引导我找到了这个解决方案。.
Hope it helps someone!
希望它可以帮助某人!
回答by Felix Cruz
This method worked for me:
这种方法对我有用:
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.layout.rootLayout);
relativeLayout.setBackgroundColor(getResources().getColor(R.color.bg_color_2));
Set id in layout xml
在布局 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"
Add color values/color.xml
添加颜色值/color.xml
<color name="bg_color_2">#ffeef7f0</color>
回答by Lo Juego
The simplest way is to add android:background="#FFFFFF"
to the main node in the layout.xml or /res/layout/activity_main.xml
:
最简单的方法是添加android:background="#FFFFFF"
到layout.xml中的主节点或者/res/layout/activity_main.xml
:
<?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>
回答by Najaf Ali
The best way using background with gradient as it does not increase app size of your app images are poison for android app so try to use it less instead of using one color as a background you can use multiple colors in one background.
使用带有渐变的背景的最佳方法,因为它不会增加应用程序图像的应用程序大小,这对 Android 应用程序来说是一种毒药,因此尽量少使用它,而不是使用一种颜色作为背景,您可以在一种背景中使用多种颜色。
回答by ashwini
You can try this in xml
sheet:
您可以在工作xml
表中尝试此操作:
android:background="@color/background_color"
回答by Sarath VK
In Layout,to change background use this.
在布局中,改变背景使用这个。
android:background="@color/your_color"
In Program can be use this. For eg: Texview background color
在程序中可以使用这个。例如:Texview 背景色
TextView tvName = (TextView) findViewById(R.id.tvName);
tvName.setBackgroundColor(getResources().getColor(R.color.your_color));
回答by Focaloid
You can use simple color resources, specified usually inside
您可以使用简单的颜色资源,通常在里面指定
res/values/colors.xml.
use
用
<color name="red">#ffff0000</color>
and use this via android:background="@color/red"
. This color can be used anywhere else too, e.g. as a text color. Reference it in XML the same way, or get it in code via
并通过android:background="@color/red"
. 这种颜色也可以用于其他任何地方,例如作为文本颜色。以同样的方式在 XML 中引用它,或者通过代码获取它
getResources().getColor(R.color.red).
You can also use any drawable resource as a background, use android:background="@drawable/mydrawable"
for this (that means 9patch drawables, normal bitmaps, shape drawables, ..).
您还可以使用任何可绘制资源作为背景,android:background="@drawable/mydrawable"
为此使用(这意味着 9patch 可绘制对象、普通位图、形状可绘制对象等)。