java代碼中實現android背景選擇的selector-StateListDrawable的應用

來源:互聯網
上載者:User

標籤:

  

首先定義一個獲得StateListDrawable對象的方法:

 
private StateListDrawable addStateDrawable(Context context,  int idNormal, int idPressed, int idFocused) {    StateListDrawable sd = new StateListDrawable();    Drawable normal = idNormal == -1 ? null : context.getResources().getDrawable(idNormal);    Drawable pressed = idPressed == -1 ? null : context.getResources().getDrawable(idPressed);    Drawable focus = idFocused == -1 ? null : context.getResources().getDrawable(idFocused);    //注意該處的順序,只要有一個狀態與之相配,背景就會被換掉    //所以不要把大範圍放在前面了,如果sd.addState(new[]{},normal)放在第一個的話,就沒有什麼效果了    sd.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, focus);    sd.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed);    sd.addState(new int[]{android.R.attr.state_focused}, focus);    sd.addState(new int[]{android.R.attr.state_pressed}, pressed);    sd.addState(new int[]{android.R.attr.state_enabled}, normal);    sd.addState(new int[]{}, normal);    return sd;}

 

其中,就如注釋中所講的addState的順序相當重要。

使用ddStateDrawable

 
//……前面對Button的聲明略去okBtn.setBackgroundDrawable(addStateDrawable(this, R.drawable.btn_normal, R.drawable.btn_selected, R.drawable.btn_selected));cancelBtn.setBackgroundDrawable(addStateDrawable(this, R.drawable.btn_normal, R.drawable.btn_selected, R.drawable.btn_selected));

  
    // Bitmap轉換成Drawable    public Drawable bitmap2Drawable(Bitmap bitmap) {        BitmapDrawable bd = new BitmapDrawable(getResources(), bitmap);        Drawable d = (Drawable) bd;        return d;    }    private StateListDrawable addStateDrawable(Bitmap idNormal, Bitmap idPressed, Bitmap idFocused) {        StateListDrawable sd = new StateListDrawable();        Drawable normal = bitmap2Drawable(idNormal);        Drawable pressed = bitmap2Drawable(idPressed);        Drawable focus = bitmap2Drawable(idFocused);        //注意該處的順序,只要有一個狀態與之相配,背景就會被換掉        //所以不要把大範圍放在前面了,如果sd.addState(new[]{},normal)放在第一個的話,就沒有什麼效果了        sd.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, focus);        sd.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed);        sd.addState(new int[]{android.R.attr.state_focused}, focus);        sd.addState(new int[]{android.R.attr.state_pressed}, pressed);        sd.addState(new int[]{android.R.attr.state_enabled}, normal);        sd.addState(new int[]{}, normal);        return sd;    }

 

   

java代碼中實現android背景選擇的selector-StateListDrawable的應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.