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


当前回答

简单的方法

安卓:背景=“@android:颜色/白色”

不需要定义任何东西。它使用android.R中预定义的颜色。

其他回答

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

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:颜色/白色”

不需要定义任何东西。它使用android.R中预定义的颜色。

以最简单的方式通过编程改变背景颜色(仅不改变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的文本颜色在代码?。

希望它能帮助到一些人!

不仅仅是给Kotlin,当你写作的时候

@color /

你可以选择任何你想要的,快速而简单:

android:background="@color/md_blue_900"

change_color setBackground getDrawable (R .[。purple_700);

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