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


当前回答

你可以试试这种方法

android:background="@color/white"

其他回答

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

使用渐变背景的最好方法,因为它不会增加应用程序的大小,图像是android应用程序的毒药,所以尽量少使用它,而不是使用一种颜色作为背景,你可以在一个背景中使用多种颜色。

这个方法对我很管用:

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>

你可以试试这种方法

android:background="@color/white"

以最简单的方式通过编程改变背景颜色(仅不改变XML):

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

唯一的要求是activity_whatever.xml中的“base”元素有一个你可以在Java中引用的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和James,谁在这里回答,有点引导我到这个解决方案,在检查了各种可能性如何设置TextView的文本颜色在代码?。

希望它能帮助到一些人!