我希望能够在我的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>

其他回答

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

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

希望这能有所帮助!

我希望能够改变背景颜色为白色在我的 最简单的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>

通过这种方式,你不需要在未来改变背景颜色很多次。

以最简单的方式通过编程改变背景颜色(仅不改变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:颜色/白色”

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

你可以尝试在xml表:

android:background="@color/background_color"