Android: Changing Background-Color of the Activity (Main View)

后端 未结 7 862
悲哀的现实
悲哀的现实 2020-11-30 04:24

I want to change the background color of my Main-View (not a Button or a Text-View) just the real background which is usually black... I got this code:

view.         


        
相关标签:
7条回答
  • 2020-11-30 04:31

    i don't know if it's the answer to your question but you can try setting the background color in the xml layout like this. It is easy, it always works

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

    You can also do more fancy things with backgrounds by creating an xml background file with gradients which are cool and semi transparent, and refer to it for other use see example below:

    the background.xml layout

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

    your layout

    <?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>
    
    0 讨论(0)
  • 2020-11-30 04:34

    Just add this below one line code in the XML file of that corresponding activity:

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

    it will help you for sure.

    0 讨论(0)
  • 2020-11-30 04:41

    You can also try and provide an Id for the main layout and change the background of that through basic manipulation and retrieval. E.g:

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

    Which can then be followed by accessing through R.id.hello.... Pretty basic and I hope this does help :)

    0 讨论(0)
  • 2020-11-30 04:48

    Try creating a method in your Activity something like...

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

    Then call it from your OnClickListener passing in whatever colour you want.

    0 讨论(0)
  • 2020-11-30 04:48

    First Method

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

    Second Method

    Add this single line after setContentView(...);

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

    Third Method

    set background color to the 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>
    

    Important Thing

    rootView.setBackgroundColor(0xFF00FF00); //after 0x the other four pairs are alpha,red,green,blue color. 
    
    0 讨论(0)
  • 2020-11-30 04:49

    I just want to add my 2 cents. I had the same goal (to change the background color from the .java class). But none of the above methods worked for me.

    Issue was, that I set the background color inside the layout .xml file with 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">
    

    So I just deleted particular line:

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

    Now I (you) can set the color from the code with:

    getWindow().getDecorView().setBackgroundColor(Color.GRAY);
    
    0 讨论(0)
提交回复
热议问题