Java 如何以编程方式将活动的背景色设置为白色?

Java 如何以编程方式将活动的背景色设置为白色?,java,android,android-activity,colors,Java,Android,Android Activity,Colors,如何通过编程将活动的背景色设置为白色??xml version=“1.0”encoding=“utf-8”> ?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:layo

如何通过编程将活动的背景色设置为白色?

?xml version=“1.0”encoding=“utf-8”>
?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>

获取所用根布局的句柄,然后设置其背景色。根布局就是您所使用的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));

我喜欢按主题着色

<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>

@颜色/自定义主题颜色
@颜色/自定义主题颜色

在活动中的
setContentView()调用之后添加这一行

getWindow().getDecorView().setBackgroundColor(Color.WHITE);

您可以使用此选项调用预定义的android颜色:

element.setBackgroundColor(android.R.color.red);
如果您想使用自己的自定义颜色,可以将自定义颜色添加到strings.xml,然后使用下面的调用

element.setBackgroundColor(R.color.mycolour);
但是,如果要在layout.xml中设置颜色,可以修改以下内容并将其添加到任何接受它的元素中

android:background="#FFFFFF"

要获取xml文件中定义的根视图,而不使用操作栏,可以使用以下方法:

View root = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
因此,要将颜色更改为白色:

root.setBackgroundResource(Color.WHITE);
为我工作。谢谢。

在您的
onCreate()方法中:

final View rootView = findViewById(android.R.id.content);
rootView.setBackgroundResource(...);
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>

#000000
请注意,您可以将
color.xml
命名为您想要的任何名称,但您可以通过代码将其称为
R.color.yourId

编辑

由于
getResources().getColor()
已被弃用,请使用
getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(MainActivity.this,R.color.main_activity_background_color));
代替。

用于活动

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中将任何内容设置为背景色,它将覆盖您试图在代码中设置的任何内容。

Mm这是一个很好的观点。不管怎样,我给出的链接很容易地回答了这个问题。我认为你没有给我正确的颜色值!!我用#ffffff得到了它这是一个很好的答案,对于我们中那些想用xml做这件事并通过谷歌搜索到这里的人来说。windowBackground和colorBackground之间有什么区别?只需设置windowBackground,它就可以正常工作了。colorBackground有什么用?@AlikElzin kilaka:不同的是,当应用程序启动时,android:windowBackground
首先会在短时间内可见,然后布局背景色就会出现。因此,如果使用两种不同的颜色,它将在屏幕上闪烁。
windowBackground
仅影响窗口背景,但
colorBackground
也影响所有视图。当我这样做时,Eclipse用“应该在这里传递解析的颜色而不是资源id:getResources().getColor(android.R.color.red)”.将最后一行更改为
root.setBackgroundColor(getResources().getColor(android.R.color.red))这个答案有效;但根据提问者的说法,它仍然不是完全有计划的。我建议阿伦库马尔在下面给出答案。我认为这个答案不正确。这不是为活动设置颜色,这将导致透支。正确的答案在下面,应该像
window.decorView.setBackgroundColor(getResolvedColor(R.color.your_color))
同意。这会在应用根布局之前更改窗口的颜色,接受的答案会更改活动布局中根元素的颜色。我认为这应该是正确的答案,如果您只想设置活动的背景颜色。当my+1更改根窗口颜色时,这肯定是最好的答案。这应该是正确的答案。接受的答案将导致透支。如果我使用第一种技术,我会得到一个警告,它实际上应该像这样访问:getResources().getColor(android.R.color.black);这个答案在低质量的帖子评论队列中,因为它只是没有解释的代码。请通过解释代码的作用以及如何回答问题来改进您的答案。请阅读。有趣的是,这清楚地将背景设置为蓝色,而不是要求的白色。
getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.main_activity_background_color));