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


当前回答

这段代码对你很有用:

android:background="#fff"

其他回答

另一种方法,进入布局->你的。xml文件->设计视图。然后进入组件树,选择你想要改变颜色的布局。在下面的组件树中有属性部分。在属性部分中选择背景(在图1中),然后单击图2。然后资源窗口将打开。从选择颜色菜单。然后选择你想要的颜色。在这里输入图像描述

你可以使用简单的颜色资源,通常在里面指定

res/values/colors.xml.

use

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

并通过android:background="@color/red"使用这个。这种颜色也可以用于其他任何地方,例如作为文本颜色。以同样的方式在XML中引用它,或者在代码中通过

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

你也可以使用任何可绘制的资源作为背景,使用android:background="@drawable/mydrawable"(这意味着9patch drawables,正常位图,形状drawables, ..)。

这个方法对我很管用:

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>

change_color setBackground getDrawable (R .[。purple_700);

当我尝试在点击时改变颜色时,这种方法是有效的。

如果你想为整个活动添加背景色

<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" />

希望这能有所帮助!