如何通过编程将 Activity 的背景颜色设置为白色?
setContentView()
调用之后添加这一行代码。getWindow().getDecorView().setBackgroundColor(Color.WHITE);
获取根布局的句柄,然后在其上设置背景颜色。根布局是您使用setContentView指定的任何布局。
setContentView(R.layout.main);
// Now get a handle to any View contained
// within the main layout you are using
View someView = findViewById(R.id.randomViewInMainLayout);
// Find the root view
View root = someView.getRootView();
// Set the color
root.setBackgroundColor(getResources().getColor(android.R.color.red));
root.setBackgroundColor(getResources().getColor(android.R.color.red));
。 - Luis Mendowindow.decorView.setBackgroundColor(getResolvedColor(R.color.your_color))
这样。 - Sotti我更喜欢按主题着色
<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>
android:windowBackground
在一瞬间可见,然后布局背景颜色接管。因此,如果使用两种不同的颜色,屏幕会闪烁。 - GoTowindowBackground
只影响窗口背景,而 colorBackground
则影响所有视图的背景。 - fdermishin?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>
换句话说,“android:background”是您想要更改的XML标签。
如果您需要动态更新背景值,请参阅以下内容:
在你的 onCreate()
方法中:
getWindow().getDecorView().setBackgroundColor(getResources().getColor(R.color.main_activity_background_color));
您需要在values文件夹中添加一个名为color.xml
的新XML文件,并在其中分配一个新的颜色属性:
color.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="main_activity_background_color">#000000</color>
</resources>
注意,您可以使用任何名称来命名color.xml
,但在代码中引用时应使用R.color.yourId
。
编辑
因为getResources().getColor()
已被弃用,请改用getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.main_activity_background_color));
。getWindow().getDecorView().findViewById( android.R.id.content ).setBackgroundColor( backgroundColor );
- BuvinJ您可以使用此功能来调用预定义的Android颜色:
element.setBackgroundColor(android.R.color.red);
如果您想使用自定义颜色之一,可以将其添加到strings.xml中,然后使用下面的代码进行调用。
element.setBackgroundColor(R.color.mycolour);
但是,如果您想在layout.xml中设置颜色,则可以修改并添加下面的内容到任何接受它的元素中。
android:background="#FFFFFF"
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"));
}
});
}
View root = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
所以,要将颜色更改为白色:
root.setBackgroundResource(Color.WHITE);
final View rootView = findViewById(android.R.id.content);
rootView.setBackgroundResource(...);
View randview = new View(getBaseContext());
randview = (View)findViewById(R.id.container);
randview.setBackgroundColor(Color.BLUE);
对我有用。谢谢。