汤姆,
确实如果你覆盖默认状态,你还必须覆盖按下和聚焦的状态.原因是默认的android drawable是一个选择器,所以用静态drawable覆盖它意味着你丢失了压缩和聚焦状态的状态信息,因为你只有一个指定的drawable.但是,实现自定义选择器非常容易.做这样的事情:
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custombutton">
android:state_focused="true"
android:drawable="@drawable/focused_button" />
android:state_pressed="true"
android:drawable="@drawable/pressed_button" />
android:state_pressed="false"
android:state_focused="false"
android:drawable="@drawable/normal_button" />
将它放在drawables目录中,并像ImageButton背景的普通drawable一样加载它.对我来说最困难的部分是设计实际图像.
编辑:
刚刚开始深入研究EditText的来源,这就是他们设置背景drawable的方式:
public EditText(/*Context context, AttributeSet attrs, int defStyle*/) {
super(/*context, attrs, defStyle*/);
StateListDrawable mStateContainer = new StateListDrawable();
ShapeDrawable pressedDrawable = new ShapeDrawable(new RoundRectShape(10,10));
pressedDrawable.getPaint().setStyle(Paint.FILL);
pressedDrawable.getPaint().setColor(0xEDEFF1);
ShapeDrawable focusedDrawable = new ShapeDrawable(new RoundRectShape(10,10));
focusedDrawable.getPaint().setStyle(Paint.FILL);
focusedDrawable.getPaint().setColor(0x5A8AC1);
ShapeDrawable defaultDrawable = new ShapeDrawable(new RoundRectShape(10,10));
defaultDrawable.getPaint().setStyle(Paint.FILL);
defaultDrawable.getPaint().setColor(Color.GRAY);
mStateContainer.addState(View.PRESSED_STATE_SET, pressedDrawable);
mStateContainer.addState(View.FOCUSED_STATE_SET, focusedDrawable);
mStateContainer.addState(StateSet.WILD_CARD, defaultDrawable);
this.setBackgroundDrawable(mStateContainer);
}
我相信你可以根据你的目的调整这个想法.这是我在上面找到的页面: