【问题标题】:How to set background color of an Activity to white programmatically?如何以编程方式将 Activity 的背景颜色设置为白色?
【发布时间】:2011-06-13 07:10:06
【问题描述】:

如何以编程方式将 Activity 的背景颜色设置为白色?

【问题讨论】:

    标签: java android android-activity colors


    【解决方案1】:

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

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

    【讨论】:

    • 同意。这会在应用根布局之前更改窗口的颜色,接受的答案会更改活动布局中根元素的颜色
    • 我觉得这个应该是正确的,如果你只是想设置activity的背景色。
    • 我的 +1 因为它改变了根窗口的颜色
    • 这绝对是最好的答案
    • 这应该是正确的答案。接受的答案会导致透支。
    【解决方案2】:

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

    【讨论】:

    • 当我这样做时,Eclipse 将其标记为“应在此处传递已解析的颜色而不是资源 ID:getResources().getColor(android.R.color.red)”。
    • 把最后一行改成root.setBackgroundColor(getResources().getColor(android.R.color.red));
    • 这个答案有效;但根据提问者的说法,它仍然不是完全程序化的。我会在下面建议 Arunkumar 的答案。
    • 我不认为这个答案是正确的。这不是为活动设置颜色,这将导致过度绘制。正确答案在下面,应该像window.decorView.setBackgroundColor(getResolvedColor(R.color.your_color))
    【解决方案3】:

    我更喜欢按主题着色

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

    【讨论】:

    • windowBackground 和 colorBackground 有什么区别?
    • 只要设置windowBackground就可以了。颜色背景有什么用?
    • @AlikElzin-kilaka:不同之处在于,当应用程序启动时,android:windowBackground 首先是可见的,有一小会儿,然后是布局背景颜色。所以如果你使用两种不同的颜色,它会在屏幕上闪烁。
    • windowBackground 仅影响窗口背景,但colorBackground 也影响所有视图。 stackoverflow.com/questions/26266221/…
    【解决方案4】:
    ?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 中的标记。

    如果需要动态更新背景值,请看以下内容:

    Exercise: Change background color, by SeekBar

    【讨论】:

    • 嗯,好点。不管怎样,我给出的链接很容易回答了这个问题。
    • 我不认为你给了我正确的颜色值!!我得到它会用#FFFFFF
    • 对于我们这些想要在 xml 中执行并通过谷歌搜索到达这里的人来说,这是一个很好的答案。
    【解决方案5】:

    您可以使用它来调用预定义的 android 颜色:

    element.setBackgroundColor(android.R.color.red);
    

    如果您想使用自己的自定义颜色之一,可以将自定义颜色添加到 strings.xml,然后使用下面的代码调用它。

    element.setBackgroundColor(R.color.mycolour);
    

    但是,如果您想在 layout.xml 中设置颜色,您可以修改以下内容并将其添加到任何接受它的元素中。

    android:background="#FFFFFF"
    

    【讨论】:

    • 如果我使用第一种技术,我会收到一条警告,说它实际上应该像这样访问:getResources().getColor(android.R.color.black);
    【解决方案6】:

    在您的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));

    【讨论】:

      【解决方案7】:

      要获得在你的 xml 文件中定义的根视图,没有操作栏,你可以使用这个:

      View root = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
      

      所以,将颜色变为白色:

      root.setBackgroundResource(Color.WHITE);
      

      【讨论】:

        【解决方案8】:
        View randview = new View(getBaseContext());
        randview = (View)findViewById(R.id.container);
        randview.setBackgroundColor(Color.BLUE);
        

        为我工作。谢谢。

        【讨论】:

        • 这个答案在低质量帖子审查队列中,因为它只是没有解释的代码。请通过解释您的代码的作用以及它如何回答问题来改进您的答案。请阅读this advice on answering programming questions helpfully
        • 有点好笑,这清楚地将背景设置为蓝色而不是要求的白色。
        【解决方案9】:
        final View rootView = findViewById(android.R.id.content);
        rootView.setBackgroundResource(...);
        

        【讨论】:

          【解决方案10】:
          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"));
                  }
              });
          }
          

          【讨论】:

            【解决方案11】:

            活动

            findViewById(android.R.id.content).setBackgroundColor(color)
            

            【讨论】:

              【解决方案12】:

              目前最好的方法当然是

              getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.main_activity_background_color));
              

              但请注意,如果您在 Designer 中将任何内容设置为背景颜色,它将覆盖您尝试在代码中设置的任何内容

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2021-10-18
                • 2023-03-26
                • 1970-01-01
                • 2015-03-04
                • 2014-06-24
                • 1970-01-01
                • 1970-01-01
                • 2021-05-20
                相关资源
                最近更新 更多