How to modify the default button state in Android without affecting the pressed and selected states?

前端 未结 2 1233
北恋
北恋 2020-12-21 10:54

I am trying to remove an ImageButton\'s background in only the default state. I\'d like the pressed and selected states to behave as usual so that they look correct on diffe

2条回答
  •  余生分开走
    2020-12-21 11:42

    StateListDrawable replace = new StateListDrawable();
    
    Drawable old = getBackground();
    replace.addState(FOCUSED_STATE_SET, old);
    replace.addState(SELECTED_STATE_SET, old);
    replace.addState(PRESSED_STATE_SET, old);
    
    replace.addState(StateSet.WILD_CARD, new ColorDrawable(Color.TRANSPARENT));
    
    if (Build.VERSION.SDK_INT >= 16) {
        setBackground(replace);
    } else {
        setBackgroundDrawable(replace);
    }
    

提交回复
热议问题