Change activity background from code

后端 未结 3 1899
情深已故
情深已故 2021-01-05 11:26

I have an activity with a background:



        
相关标签:
3条回答
  • 2021-01-05 12:08
    LinearLayout  vi = (LinearLayout ) findViewById(<id>)
    vi.setBackgroundResource(R.drawable.<id2>);
    

    you need to provide the id to your LinearLayout in XML as well....

    0 讨论(0)
  • 2021-01-05 12:15

    In java we do like this

    Give id to LinearLayout

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

    Then in code

    LinearLayout layout=(LinearLayout)findViewById(R.id.parentLayout);
    
    layout.setBackgroundColor(Color.BLUE);  Or
    layout.setBackgroundDrawable(d); Or
    layout.setBackgroundResource(resid);
    
    0 讨论(0)
  • 2021-01-05 12:25

    first add LinearLayout id as in your layout xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/linearLayoutid" 
    android:minWidth="25px"
    android:minHeight="25px"
    android:background="@drawable/background_fingerboard" 
    

    and in code part set background as::

    LinearLayout  linearLayout = (LinearLayout) findViewById(R.id.linearLayoutid);
     linearLayout.setBackgroundResource(R.drawable.background_fingerboard);
    
    0 讨论(0)
提交回复
热议问题