Android 动态改变背景 LinearLayout

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3512680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:33:03  来源:igfitidea点击:

Dynamically change the background LinearLayout

android

提问by user413881

How to dynamically change the background LinearLayout?

如何动态改变背景LinearLayout?

回答by Sephy

Did you try one of these :

您是否尝试过其中之一:

yourLayout.setBackgroundColor(int color);
yourLayout.setBackgroundDrawable(Drawable d);
yourLayout.setBackgroundResource(int resid);

and if does not refresh on its own, this should give it a boost :

如果不自行刷新,这应该会提升它:

   yourLayout.invalidate();

回答by Kevin Coppock

I'm at work right now, so I can't test this, but I believe this should work:

我现在正在工作,所以我无法测试这个,但我相信这应该有效:

LinearLayout linLay = (LinearLayout) findViewById(R.id.theLinearLayoutId);

//set background to a color
linLay.setBackgroundColor(Color.parseColor("#404040"));

//set background to a drawable
linLay.setBackgroundDrawable(drawableItem);

//set background to a resource
linLay.setBackgroundResource(R.id.backgroundResource);