首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android:更改活动的背景颜色(主视图)

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

Stack Overflow用户
提问于 2012-01-22 20:34:15
回答 9查看 129.3K关注 0票数 26

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

代码语言:javascript
复制
view.setBackgroundColor(0xfff00000);

这是在一个OnClickListener中,但它只是改变了按钮的背景。

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2012-01-22 20:42:48

尝试在您的Activity中创建一个方法,类似于...

代码语言:javascript
复制
public void setActivityBackgroundColor(int color) {
    View view = this.getWindow().getDecorView();
    view.setBackgroundColor(color);
}

然后从你的OnClickListener调用它,传入你想要的任何颜色。

票数 64
EN

Stack Overflow用户

发布于 2012-01-22 21:48:15

我不知道这是否是您问题的答案,但是您可以尝试像这样设置xml布局中的背景色。这很简单,它总是有效的。

代码语言:javascript
复制
<?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>

你还可以通过创建一个带有渐变的xml背景文件来对背景做更多花哨的事情,这些渐变很酷,而且是半透明的,并参考它以用于其他用途请参见下面的示例:

background.xml布局

代码语言:javascript
复制
<?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>

你的布局

代码语言:javascript
复制
<?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>
票数 9
EN

Stack Overflow用户

发布于 2014-02-15 20:09:22

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

代码语言:javascript
复制
android:background="@android:color/black" 

这肯定会对你有帮助。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8961071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档