Android 按钮选择状态,如何修改Android中的默认按钮状态而不影响按下和选择的状态?...

汤姆,

确实如果你覆盖默认状态,你还必须覆盖按下和聚焦的状态.原因是默认的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);

}

我相信你可以根据你的目的调整这个想法.这是我在上面找到的页面:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值