setBackground(),setBackgroundResource(),setBackgroundColor(),setBackgroundDrawable()的區別和用法

已經畢業的C先生發表於2017-10-21

setBackground(),setBackgroundResource(),setBackgroundColor()和setBackgroundDrawable()這幾個方法都可以對控制元件的顏色進行設定,setBackground(),setBackgroundResource()和setBackgroundDrawable()可以對背景的樣式進行設定,但他們之間又有一定的區別

  • SetBackground(Drawable background)其引數為一個Drawable物件,目的是設定view的背景圖片,Drawable物件可以這樣獲取getResources().getDrawable(R.drawable.xx),還可以是Context.getResource().getColor(R.color.white)

  • setBackgroundColor(int color)其引數為一個顏色值,其目的是設定一個view的背景顏色

  • setBackgroundDrawable(Drawable background)和SetBackground有異曲同工之妙,都是通過傳入一個Drawable物件設定view控制元件的背景圖片

  • setBackgroundResource(int resid)它也是設定一個view的背景圖片,只不過傳入的是一個drawable的id值或者color顏色值

setBackground和setBackgroundDrawable方法的區別:兩者都是傳入一個Drawable物件,但setBackground實在API16以上才提供的方法,在API16及以下則是隻能使用setBackgroundDrawable的方法,如果在API16 以上使用setBackgroundDrawable方法則會提示該方法過時(其實它還能用,只不過android官方不建議你用,給這個方法畫了個橫線,可以在相容PAI16 以下系統的APP裡使用這個方法。),對於這種情況,可以通過對系統判斷來決定使用什麼方法,給個小程式碼:

//在API16以前使用setBackgroundDrawable,在API16以後使用setBackground 
// API16<---->Android 4.1
 private void setBackgroundOfVersion(View view, Drawable drawable) {  
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {  
            //Android系統大於等於API16,使用setBackground  
            view.setBackground(drawable);  
        } else {  
            //Android系統小於API16,使用setBackgroundDrawable  
            view.setBackgroundDrawable(drawable);  
        }  
    }  

setBackgroundResource和setBackgroundColor的區別:一開始我也不是很明白,看了一些大神的部落格後發現還是有區別的,setBackgroundResource設定的是最底層的顏色,當改變完顏色以後,如果佈局在xml檔案中預設顏色是white,會被white遮蓋掉。setBackgroundColor設定的是中間層的顏色,相當於XML檔案裡的顏色setBackgroundColor(context.getResouce().getColor(R.color.XXX))可以理解為改變的是最上層的顏色,不管xml佈局中的顏色是什麼色,使用了setBackgroundColor,就會在佈局顏色上層刷上顏色,所以就會顯色。
這只是我的個人理解,如果哪裡有問題,希望在評論區指出來,萬分感謝!

關於setBackground和setBackgroundDrawable傳入的物件的書寫問題:如果是以下格式不會有問題:btn.setBackground(getResources().getDrawable(R.drawable.buttonstyle));
btn.setBackgroundDrawable(getResources().getDrawable(R.drawable.buttonstyle));

若是這種書寫格式則會有問題:btn.setBackground(getDrawable(R.drawable.buttonstyle));
btn.setBackgroundDrawable(getDrawable(R.drawable.buttonstyle));
,經過測試發現,在android5.0及以上的系統不會有問題,和上面的格式一樣,但是在android4.4及以下的系統則會出現閃退現象,所以這種書寫格式在android4.4及以下的系統不適用,建議在寫程式碼的時候使用第一種書寫格式,避免不必要的的問題!

關於setBackgroundColor:如果在XML檔案裡使用background屬性設定了樣式,改變原有樣式,如果在java程式碼裡使用setBackgroundColor的方法則會把原來XML裡的樣式給覆蓋掉,變成預設樣式。如果涉及到樣式的修改,一般不用setBackgroundColor方法。

給個自己寫的小Demo程式碼:

XML檔案activity_button.xml

<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.example.a14392.aboutbackground.ButtonBackground">

    <TextView
        android:layout_marginTop="36dp"
        android:textSize="18sp"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="關於設定background的問題"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_1"
        android:layout_width="250dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:layout_marginTop="24dp"
        android:background="@drawable/buttonstyle"
        android:text="setBackground屬性"
        android:textAllCaps="false"
        android:textColor="#ffffff"
        android:textSize="18sp" />

    <Button
        android:id="@+id/btn_2"
        android:layout_width="250dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:layout_marginTop="24dp"
        android:text="setBackgroundResource屬性"
        android:textSize="18sp"
        android:textAllCaps="false"
        android:textColor="#ffffff"
        android:background="@drawable/buttonstyle"/>
    <Button
        android:id="@+id/btn_3"
        android:layout_width="250dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:layout_marginTop="24dp"
        android:text="setBackgroundColor屬性"
        android:textSize="18sp"
        android:textAllCaps="false"
        android:textColor="#ffffff"
        android:background="@drawable/buttonstyle"/>
    <Button
        android:id="@+id/btn_4"
        android:layout_width="250dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:layout_marginTop="24dp"
        android:text="setBackgroundDrawable屬性"
        android:textSize="18sp"
        android:textAllCaps="false"
        android:textColor="#ffffff"
        android:background="@drawable/buttonstyle"/>
</LinearLayout>

drawable資料夾裡的兩個樣式檔案buttonstyle.xml和buttonstyle2.xml

buttonstyle.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="6dp"/>
            <solid android:color="#07c3f7"/>
        </shape>
    </item>
</selector>

buttonstyle2.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="6dp"/>
            <solid android:color="#0d8ced"/>
        </shape>
    </item>
</selector>

ButtonBackground.java

public class ButtonBackground extends AppCompatActivity implements View.OnClickListener {

    public Button btn1,btn2,btn3,btn4;
    int b1=0,b2=0,b3=0,b4=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button);
        findview();
    }

    void findview(){
        btn1=(Button) findViewById(R.id.btn_1);
        btn2=(Button) findViewById(R.id.btn_2);
        btn3=(Button) findViewById(R.id.btn_3);
        btn4=(Button) findViewById(R.id.btn_4);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_1:
                if(b1==0){
                    btn1.setBackground(getResources().getDrawable(R.drawable.buttonstyle2));
                    /*btn1.setBackground(getDrawable(R.drawable.buttonstyle2));//這種格式在android4.4及以下的系統會出現閃退問題*/
                    b1=1;
                }else {
                    btn1.setBackground(getResources().getDrawable(R.drawable.buttonstyle));
                    /*btn1.setBackground(getDrawable(R.drawable.buttonstyle));//這種格式在android4.4及以下的系統會出現閃退問題*/
                    b1=0;
                }
                break;
            case R.id.btn_2:
                if(b2==0){
                    btn2.setBackgroundResource(R.drawable.buttonstyle2);
                    b2=1;
                }else {
                    btn2.setBackgroundResource(R.drawable.buttonstyle);
                    b2=0;
                }
                break;
            case R.id.btn_3:
                if(b3==0){
                    btn3.setBackgroundColor(Color.parseColor("#0d8ced"));
                    b3=1;
                }else {
                    btn3.setBackgroundColor(Color.parseColor("#07c3f7"));
                    b3=0;
                }
                break;
            case R.id.btn_4:
                if(b4==0){
                    btn4.setBackgroundDrawable(getResources().getDrawable(R.drawable.buttonstyle));
                   /* btn4.setBackgroundDrawable(getDrawable(R.drawable.buttonstyle));這種格式在android4.4及以下的系統會出現閃退問題*/
                    b4=1;
                }else {
                    btn4.setBackgroundDrawable(getResources().getDrawable(R.drawable.buttonstyle2));
                   /* btn4.setBackgroundDrawable(getDrawable(R.drawable.buttonstyle));這種格式在android4.4及以下的系統會出現閃退問題*/
                    b4=0;
                }
                break;
            default:break;
        }
    }
}

看一下效果圖:
點選前的效果
點選後的效果

如有錯誤與不足,歡迎評論指出,謝謝!!!

相關文章