2010-05-01 130 views

回答

320

您需要使用android:背景属性,如

android:background="@color/white" 

你也需要在strings.xml中

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

编辑添加值的白色:2012年11月18日

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

如:

enter image description here

+1

谢谢你现在的作品 – ryan 2010-05-04 02:15:19

+0

你如何指定阿尔法?添加额外的FF等不起作用。 – Jonny 2012-11-14 05:42:48

+0

@Jonny前两个字母提供了alpha值。 – 2012-11-18 12:18:12

147

您还可以使用

android:background="#ffffff" 

在你的XML布局或/res/layout/activity_main.xml,或者您可以通过添加

android:theme="@android:style/Theme.Light" 
改变你的AndroidManifest.xml主题

添加到您的活动标签。

如果要动态改变的背景下,使用

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

这是正确的答案,因为它有主题,我相信。单线不是最简单的方式,而是将整个风格传播到一切的一种方式。风格是你如何实现一条线甚至是复杂的变化。此外,样式适用于偏好,不像一般属性。 (虽然仍然有一些奇怪) – 2017-03-24 14:39:22

55

最简单的方法

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

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

+7

和编程:'context.getResources()。getColor(android.R.color.white)' – Paschalis 2013-07-25 22:30:00

+1

谢谢,这实际上是我在找。在我的情况下,我必须用Java来完成。 – Armfoot 2014-08-30 14:16:26

+0

以及支持库:[链接](http://stackoverflow.com/a/31590927/776345) – Paschalis 2016-05-13 22:51:42

3

最简单的方法是添加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> 
+0

这不是一个好主意,因为它会导致不必要的GPU透支(首先,窗口背景将被绘制并且然后是TextView的背景)。请参阅:http://www.curious-creature.com/docs/android-performance-case-study-1.html – 2015-06-29 06:28:16

8

要更改最简单的方式的背景颜色可以通过编程方式(独占 - 没有XML的变化):

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

唯一的要求是,在你的activity_whatever.xml“基地”元素都有,你可以在Java中引用ID(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和詹姆斯在这里回答说,在检查了How to set the text color of TextView in code?中的各种可能性之后,我带着这种解决方案。

希望它可以帮助别人!

0

,你可以尝试用这种方式

android:background="@color/white" 
3

此方法为我工作:

​​

集ID在布局XML

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

的另一种方式,去布局 - >您.xml文件 - >设计视图。然后去你想改变颜色组件树和选择布局。在下面的组件树中有属性部分。在属性部分(图片部分1)中选择背景。然后点击图片中的第2部分。然后资源窗口将打开。从该选择颜色菜单。然后选择你想要的颜色。 enter image description here

2

可以在xml片试试这个:

android:background="@color/background_color" 
0

布局,改变背景使用此。

android:background="@color/your_color" 

In Program can be use this。对于如:Texview背景颜色

TextView tvName = (TextView) findViewById(R.id.tvName); 
tvName.setBackgroundColor(getResources().getColor(R.color.your_color)); 
0

您可以使用简单的颜色资源,通常规定内

res/values/colors.xml. 

使用

<color name="red">#ffff0000</color> 

,并通过android:background="@color/red"使用。这种颜色也可用于其他地方,例如作为文字颜色。参考它在XML以同样的方式,或通过

getResources().getColor(R.color.red). 

得到它的代码,您也可以使用任何可绘制资源为背景,使用android:background="@drawable/mydrawable"这个(这意味着9patch绘项目,正常位图,形状可绘制.. )。

0

有些时候,文本与背景具有相同的颜色,请尝试将android:background =“#CCCCCC”放入列表视图属性中,您可以看到它。

0

机器人:背景=“#64B5F6”
您可以后“#”根据自己的规格还是需要根据您希望如何使用它们改变数值。
这是一个示例代码:

<TextView 
 
     android:text="Abir" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:textSize="24sp" 
 
     android:background="#D4E157" />

谢谢:)

0
  1. 去activity_main.xml中
  2. 有设计视图/和文本视图。
  3. 选择文字
  4. 写代码了:

    android:background="@color/colorAccent" 
    
2

使用背景渐变,因为它不会增加您的应用图片的应用规模,最好的办法是Android应用的毒药所以尝试使用它少,而不是使用一种颜色作为背景,您可以在一个背景中使用多种颜色。 enter image description here enter image description here

enter image description hereenter image description here

0

如果您想背景颜色添加到整个活动

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#1de9b6" 
    tools:context="com.example.abc.myapplication.MainActivity"> 
</RelativeLayout> 

如果您想使用后台视图

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Andrios" 
    android:background="@color/colorAccent" /> 

希望这可以帮助!