【发布时间】:2011-06-13 07:10:06
【问题描述】:
如何以编程方式将 Activity 的背景颜色设置为白色?
【问题讨论】:
标签: java android android-activity colors
如何以编程方式将 Activity 的背景颜色设置为白色?
【问题讨论】:
标签: java android android-activity colors
在您的活动中添加这一行,在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));
window.decorView.setBackgroundColor(getResolvedColor(R.color.your_color))
我更喜欢按主题着色
<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 首先是可见的,有一小会儿,然后是布局背景颜色。所以如果你使用两种不同的颜色,它会在屏幕上闪烁。
windowBackground 仅影响窗口背景,但colorBackground 也影响所有视图。 stackoverflow.com/questions/26266221/…
?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 中的标记。
如果需要动态更新背景值,请看以下内容:
【讨论】:
您可以使用它来调用预定义的 android 颜色:
element.setBackgroundColor(android.R.color.red);
如果您想使用自己的自定义颜色之一,可以将自定义颜色添加到 strings.xml,然后使用下面的代码调用它。
element.setBackgroundColor(R.color.mycolour);
但是,如果您想在 layout.xml 中设置颜色,您可以修改以下内容并将其添加到任何接受它的元素中。
android:background="#FFFFFF"
【讨论】:
在您的onCreate() 方法中:
getWindow().getDecorView().setBackgroundColor(getResources().getColor(R.color.main_activity_background_color));
您还需要在 values 文件夹中添加一个名为 color.xml 的新 XML 文件并在那里分配一个新的颜色属性:
颜色.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));
。
【讨论】:
要获得在你的 xml 文件中定义的根视图,没有操作栏,你可以使用这个:
View root = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
所以,将颜色变为白色:
root.setBackgroundResource(Color.WHITE);
【讨论】:
View randview = new View(getBaseContext());
randview = (View)findViewById(R.id.container);
randview.setBackgroundColor(Color.BLUE);
为我工作。谢谢。
【讨论】:
final View rootView = findViewById(android.R.id.content);
rootView.setBackgroundResource(...);
【讨论】:
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"));
}
});
}
【讨论】:
活动
findViewById(android.R.id.content).setBackgroundColor(color)
【讨论】:
目前最好的方法当然是
getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.main_activity_background_color));
但请注意,如果您在 Designer 中将任何内容设置为背景颜色,它将覆盖您尝试在代码中设置的任何内容。
【讨论】: