我希望能够以最简单的方式在我的安卓应用中将背景颜色更改为白色。
你需要使用 android:background 属性,例如:
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
中更改主题,或者通过在AndroidManifest.xml中添加来更改主题
android:theme="@android:style/Theme.Light"
将此标签添加到您的活动标记中。
如果您想实现动态更改背景,请使用
YourView.setBackgroundColor(Color.argb(255, 255, 255, 255));
android:background="@android:color/white"
无需定义任何东西。它使用android.R
中预定义的颜色。
context.getResources().getColor(android.R.color.white)
的意思是获取Android资源中白色颜色的值。 - Paschalis最简单的以编程方式更改背景颜色的方法(仅限代码 - 不涉及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文本颜色的各种可能性后给了我灵感。
希望能帮到有需要的人!
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>
<?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>
xml
表中尝试此操作:android:background="@color/background_color"
<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>
在布局中,要更改背景,请使用以下内容。
android:background="@color/your_color"
TextView tvName = (TextView) findViewById(R.id.tvName);
tvName.setBackgroundColor(getResources().getColor(R.color.your_color));
android:background="@android:color/white"
更加方便,这是预定义的,并且不需要您在 strings.xml 中添加任何内容。 - Greg Hewgill/res/layout/activity_main.xml
添加到答案中,以添加元素android:background
? - 030