【问题标题】:Android: Changing Background-Color of the Activity (Main View)Android:更改活动的背景颜色(主视图)
【发布时间】:2012-02-16 04:17:50
【问题描述】:

我想改变我的主视图(不是按钮或文本视图)的背景颜色,只是通常是黑色的真实背景......我得到了这个代码:

view.setBackgroundColor(0xfff00000);

这是在OnClickListener 中,但它只是改变了按钮的背景。

【问题讨论】:

标签: java android colors


【解决方案1】:

尝试在您的 Activity 中创建一个方法,例如...

public void setActivityBackgroundColor(int color) {
    View view = this.getWindow().getDecorView();
    view.setBackgroundColor(color);
}

然后从你的 OnClickListener 调用它,传入你想要的任何颜色。

【讨论】:

  • 我打开了一个带有文本框的对话框界面。单击“确定”时,我想更改颜色。我尝试了这段代码,但没有成功。是不是因为我的视图现在是在对话框界面而不是主要活动本身上?
  • 我想通了。如果有人处于类似情况,只需在您的函数中执行此操作: LinearLayout main = (LinearLayout) findViewById(R.id.myScreenMain); main.setBackgroundColor(color);
  • @Naveen :抱歉,我没有在上面看到您的 cmets。如果您想吸引某人的注意力,请以@ 开始您的评论,然后以我在此评论中为您所做的方式使用stackoverflow 用户名。然后它将显示在用户的收件箱中。很高兴您找到了解决问题的方法。
  • @Squonk :非常感谢您的帮助。我会记住这一点的:)
【解决方案2】:

我不知道这是否是您问题的答案,但您可以尝试像这样在 xml 布局中设置背景颜色。这很简单,它总是有效的

<?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="0xfff00000"

  >


<TextView

    android:id="@+id/text_view"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/hello"

    />



</LinearLayout>

您还可以通过创建具有酷炫和半透明渐变的 xml 背景文件来对背景做更多花哨的事情,并参考它以供其他用途,请参见下面的示例:

background.xml 布局

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape>
        <gradient
            android:angle="90"
            android:startColor="#f0000000"
            android:endColor="#ff444444"
            android:type="linear" />
    </shape>
</item>
</selector>

你的布局

<?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="@layout/background"


    >


<TextView

    android:id="@+id/text_view"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/hello"

    />



</LinearLayout>

【讨论】:

  • 即使这没有回答 OPs 的问题,它也回答了我的问题。 +1
  • 渐变背景的建议很棒。希望我能给你超过 +1。
【解决方案3】:

只需在相应活动的 XML 文件中添加以下一行代码即可:

android:background="@android:color/black" 

它肯定会帮助你。

【讨论】:

    【解决方案4】:

    第一种方法

     View someView = findViewById(R.id.randomViewInMainLayout);// get Any child View
    
      // Find the root view
      View root = someView.getRootView()
    
      // Set the color
      root.setBackgroundColor(getResources().getColor(android.R.color.red));
    

    第二种方法

    在 setContentView(...) 之后添加这一行;

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

    第三种方法

    为 rootView 设置背景颜色

    <?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/rootView"
    </LinearLayout>
    

    重要的事情

    rootView.setBackgroundColor(0xFF00FF00); //after 0x the other four pairs are alpha,red,green,blue color. 
    

    【讨论】:

      【解决方案5】:

      您还可以尝试为主要布局提供一个 ID,并通过基本操作和检索来更改其背景。例如:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/hello"
      

      然后可以通过 R.id.hello....

      【讨论】:

        【解决方案6】:

        我只想加我的 2 美分。 我有相同的目标(从 .java 类更改背景颜色)。但是以上方法都不适合我。

        问题是,我在布局 .xml 文件中使用android:background="@color/colorGray" 设置背景颜色:

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@color/colorGray">
        

        所以我只是删除了特定的行:

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        

        现在我(你)可以从代码中设置颜色:

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

        【讨论】:

          【解决方案7】:

          只需转到 activity_main.xml 文件。您将在右侧看到一个属性面板,在那里您会找到一个名为“背景”的属性。你可以在那里设置颜色。

          【讨论】:

            【解决方案8】:

            如果您将完整代码放在这里,我可以帮助您。如果您在 XML 中设置侦听器并在 View 上调用设置的背景颜色,那么它将更改视图的背景颜色,这意味着它是您的 Botton,所以将您的侦听器放在您的活动中,然后更改视图的颜色

            【讨论】:

              【解决方案9】:

              试试这个:

              <?xml version="1.0" encoding="utf-8"?>
              <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:app="http://schemas.android.com/apk/res-auto"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  tools:context=".MainActivity">
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:background="@color/colorPrimaryDark"
              
              
              </androidx.constraintlayout.widget.ConstraintLayout>
                  />

              【讨论】:

                【解决方案10】:

                你应该写在背景我的xml中

                android:background="@drawable/imgname"
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2018-09-14
                  • 1970-01-01
                  • 2013-10-19
                  • 1970-01-01
                  • 2012-09-22
                  • 2011-10-05
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多