How to set background color of an Activity to whit

2019-01-03 12:26发布

How can I set the background color of an Activity to white programatically?

11条回答
相关推荐>>
2楼-- · 2019-01-03 12:58

I prefer coloring by theme

<style name="CustomTheme" parent="android:Theme.Light">
    <item name="android:windowBackground">@color/custom_theme_color</item>
    <item name="android:colorBackground">@color/custom_theme_color</item>
</style>
查看更多
爷的心禁止访问
3楼-- · 2019-01-03 13:00
?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="#FFFFFF"
android:id="@+id/myScreen"
</LinearLayout>

In other words, "android:background" is the tag in the XML you want to change.

If you need to dynamically update the background value, see the following:

Exercise: Change background color, by SeekBar

查看更多
我想做一个坏孩纸
4楼-- · 2019-01-03 13:00

I was having a similar problem, and I followed the steps mentioned in the answers given above. I'll share what worked for me.

I was using a RecyclerView, and had set a background color for the item. However, it looked weird when the number of items didn't fit the screen

My initial layout

I then used the function

public void setActivityBackgroundColor(int color) {
        View view = this.getWindow().getDecorView();
        view.setBackgroundColor(color);
    }

And called the function setActivityBackgroundColor(R.color.colorAccent); in my code in another function. That led to this layout. Pretty weird.

I finally fixed it by following what KitKat suggested.

public void setActivityBackgroundColor(int color) {
        View view = this.getWindow().getDecorView();
        view.setBackgroundResource(color);
    }

This finally fixed it.

EDIT: The solution started throwing an error if the RecyclerView was not populated. In that case, I set a flag to check if the view is empty or not.

 public void setActivityBackgroundColor(int color) {
        View view = this.getWindow().getDecorView();
        if (dataIsNull) {
            view.setBackgroundColor(color);
        }
        else {
            view.setBackgroundResource(color);
        }
    }

It works like a charm now.

查看更多
趁早两清
5楼-- · 2019-01-03 13:07

Add this single line in your activity, after setContentView() call

getWindow().getDecorView().setBackgroundColor(Color.WHITE);
查看更多
一夜七次
6楼-- · 2019-01-03 13:08
Button btn;
View root;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button)findViewById(R.id.button);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            root =findViewById(R.id.activity_main).getRootView();
            root.setBackgroundColor(Color.parseColor("#FFFFFF"));
        }
    });
}
查看更多
叼着烟拽天下
7楼-- · 2019-01-03 13:12
View randview = new View(getBaseContext());
randview = (View)findViewById(R.id.container);
randview.setBackgroundColor(Color.BLUE);

worked for me. thank you.

查看更多
登录 后发表回答