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


当前回答

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

其他回答

最简单的方法是在layout.xml或/res/layout/activity_main.xml的主节点中添加android:background="#FFFFFF":

<?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):

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的文本颜色在代码?。

希望它能帮助到一些人!

change_color setBackground getDrawable (R .[。purple_700);

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

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

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, ..)。

这段代码对你很有用:

android:background="#fff"