Android-在XML和Java代码中设置背景在不同状态的效果: <selector>/StateListDrawable
2014-11-12 09:59
531 查看
It's true that if you override the default state you also have to override the pressed and focused states. The reason is that the default android drawable is a selector, and so overriding it with a static drawable means that you lose the state information for pressed and focused states as you only have one drawable specified for it. It's super easy to implement a custom selector, though. Do something like this:
Put this in your drawables directory, and load it like a normal drawable for the background of the ImageButton. The hardest part for me is designing the actual images.
Edit:
Just did some digging into the source of EditText, and this is how they set the background drawable:
Via: http://stackoverflow.com/questions/3859144/how-to-modify-the-default-button-state-in-android-without-affecting-the-pressed
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/custombutton"> <item android:state_focused="true" android:drawable="@drawable/focused_button" /> <item android:state_pressed="true" android:drawable="@drawable/pressed_button" /> <item android:state_pressed="false" android:state_focused="false" android:drawable="@drawable/normal_button" /> </selector>
Put this in your drawables directory, and load it like a normal drawable for the background of the ImageButton. The hardest part for me is designing the actual images.
Edit:
Just did some digging into the source of EditText, and this is how they set the background 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(new int[] { android.R.attr.state_pressed }, pressedDrawable); mStateContainer.addState(View.PRESSED_STATE_SET, pressedDrawable); mStateContainer.addState(View.FOCUSED_STATE_SET, focusedDrawable); mStateContainer.addState(StateSet.WILD_CARD, defaultDrawable); this.setBackgroundDrawable(mStateContainer); }
Via: http://stackoverflow.com/questions/3859144/how-to-modify-the-default-button-state-in-android-without-affecting-the-pressed
相关文章推荐
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- JAVA代码设置selector不同状态下的背景颜色
- Android中View绘制不同状态背景图片原理以及StateListDrawable使用详解
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- StateListDrawable与<selector>的各种状态
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中View绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- 【安卓开发】JAVA代码设置selector不同状态下的背景颜色
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解