有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android:更改活动的背景颜色(主视图)

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

view.setBackgroundColor(0xfff00000);

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


共 (6) 个答案

  1. # 1 楼答案

    第一种方法

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

    第三种方法

    将背景色设置为根视图

    <?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. 
    
  2. # 2 楼答案

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

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

    然后从你的听众那里用你想要的任何颜色来称呼它

  3. # 3 楼答案

    您还可以尝试为主布局提供一个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…访问。。。。非常基本,我希望这会有所帮助:)

  4. # 4 楼答案

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

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

    这肯定会对你有帮助

  5. # 5 楼答案

    我只想加上我的2美分。 我有同样的目标(改变.java类的背景色)。但上述方法对我都不起作用

    问题是,我在布局中设置了背景色。包含android:background="@color/colorGray"的xml文件:

    <?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);
    
  6. # 6 楼答案

    我不知道这是否是您问题的答案,但您可以尝试像这样在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背景文件(渐变是很酷的、半透明的),并将其用于其他用途,从而在背景中做更多有趣的事情。请参见下面的示例:

    背景。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>