Android:更改活动的背景颜色(主视图)

mor*_*tzg 23 java android colors

我想改变主视图(不是按钮或文本视图)的背景颜色,只是通常是黑色的真实背景...我得到了这段代码:

view.setBackgroundColor(0xfff00000);
Run Code Online (Sandbox Code Playgroud)

这是在里面OnClickListener,但它只是改变了Button的背景.

Squ*_*onk 57

尝试在你的Activity东西中创建一个方法...

public void setActivityBackgroundColor(int color) {
    View view = this.getWindow().getDecorView();
    view.setBackgroundColor(color);
}
Run Code Online (Sandbox Code Playgroud)

然后从您的OnClickListener中调用它,传入您想要的任何颜色.


you*_*oua 9

我不知道这是你的问题的答案,但你可以尝试在这样的xml布局中设置背景颜色.它很容易,它总是有效的

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

 android:background="0xfff00000"

  >


<TextView

    android:id="@+id/text_view"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/hello"

    />



</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

你也可以通过创建一个渐变的xml背景文件来做更多有趣的事情,这些渐变是很酷和半透明的,并参考它用于其他用途,参见下面的例子:

background.xml布局

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape>
        <gradient
            android:angle="90"
            android:startColor="#f0000000"
            android:endColor="#ff444444"
            android:type="linear" />
    </shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)

你的布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

 android:background="@layout/background"


    >


<TextView

    android:id="@+id/text_view"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/hello"

    />



</LinearLayout>
Run Code Online (Sandbox Code Playgroud)


Rak*_*ari 7

只需在相应活动的XML文件中的一行代码下面添加:

android:background="@android:color/black" 
Run Code Online (Sandbox Code Playgroud)

它肯定会对你有所帮助.