我希望能够以最简单的方式将我的Android应用程序中的背景颜色更改为白色。如何在Android应用程序中更改背景颜色
回答
您需要使用android:背景属性,如
android:background="@color/white"
你也需要在strings.xml中
<color name="white">#FFFFFF</color>
编辑添加值的白色:2012年11月18日
8字母颜色代码的前两个字母提供了alpha值,如果您使用的是html 6字母颜色表示法,则颜色是不透明的。
如:
您还可以使用
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));
这是正确的答案,因为它有主题,我相信。单线不是最简单的方式,而是将整个风格传播到一切的一种方式。风格是你如何实现一条线甚至是复杂的变化。此外,样式适用于偏好,不像一般属性。 (虽然仍然有一些奇怪) – 2017-03-24 14:39:22
最简单的方法
android:background="@android:color/white"
不需要定义任何东西。它使用android.R
中的预定义颜色。
最简单的方法是添加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>
这不是一个好主意,因为它会导致不必要的GPU透支(首先,窗口背景将被绘制并且然后是TextView的背景)。请参阅:http://www.curious-creature.com/docs/android-performance-case-study-1.html – 2015-06-29 06:28:16
要更改最简单的方式的背景颜色可以通过编程方式(独占 - 没有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?中的各种可能性之后,我带着这种解决方案。
希望它可以帮助别人!
,你可以尝试用这种方式
android:background="@color/white"
此方法为我工作:
集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>
的另一种方式,去布局 - >您.xml文件 - >设计视图。然后去你想改变颜色组件树和选择布局。在下面的组件树中有属性部分。在属性部分(图片部分1)中选择背景。然后点击图片中的第2部分。然后资源窗口将打开。从该选择颜色菜单。然后选择你想要的颜色。 enter image description here
可以在xml
片试试这个:
android:background="@color/background_color"
布局,改变背景使用此。
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));
您可以使用简单的颜色资源,通常规定内
res/values/colors.xml.
使用
<color name="red">#ffff0000</color>
,并通过android:background="@color/red"
使用。这种颜色也可用于其他地方,例如作为文字颜色。参考它在XML以同样的方式,或通过
getResources().getColor(R.color.red).
得到它的代码,您也可以使用任何可绘制资源为背景,使用android:background="@drawable/mydrawable"
这个(这意味着9patch绘项目,正常位图,形状可绘制.. )。
有些时候,文本与背景具有相同的颜色,请尝试将android:background =“#CCCCCC”放入列表视图属性中,您可以看到它。
机器人:背景=“#64B5F6”
您可以后“#”根据自己的规格还是需要根据您希望如何使用它们改变数值。
这是一个示例代码:
<TextView
android:text="Abir"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:background="#D4E157" />
谢谢:)
- 去activity_main.xml中
- 有设计视图/和文本视图。
- 选择文字
写代码了:
android:background="@color/colorAccent"
如果您想背景颜色添加到整个活动
<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" />
希望这可以帮助!
谢谢你现在的作品 – ryan 2010-05-04 02:15:19
你如何指定阿尔法?添加额外的FF等不起作用。 – Jonny 2012-11-14 05:42:48
@Jonny前两个字母提供了alpha值。 – 2012-11-18 12:18:12