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


当前回答

你需要使用android:background属性,例如

android:background="@color/white"

此外,您还需要在strings.xml中为white添加一个值

<color name="white">#FFFFFF</color>

编辑:2012年11月18日

8字母颜色代码的前两个字母提供alpha值,如果你使用html 6字母颜色符号,颜色是不透明的。

例如:

其他回答

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

希望它能帮助到一些人!

我希望能够改变背景颜色为白色在我的 最简单的Android应用程序。

题目说的是最简单的方法,所以在这里。

在所有父视图中设置parentViewStyle。像大多数家长视图你的活动,片段和对话框。

<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:背景= " # 64 b5f6 " 您可以根据自己的规范或需要更改'#'后的值,这取决于您想如何使用它们。 下面是一个示例代码:

< TextView android:文本= " Abir " android: layout_width = " match_parent " android: layout_height = " wrap_content " android: textSize = " 24 sp " android:背景= " # D4E157 " / >

谢谢!

有时,文本与背景颜色相同,尝试用android:background="#CCCCCC"进入listview属性,你会看到。

在布局中,改变背景使用这个。

android:background="@color/your_color"

在程序中可以使用这个。例如:Texview背景色

 TextView tvName = (TextView) findViewById(R.id.tvName);
 tvName.setBackgroundColor(getResources().getColor(R.color.your_color));