在android中,可以设置一个button控件各状态下的样式。
首先,在res/drawable中定义mbutton.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/btnfocused" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/btnfocusedpressed" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/btnpressed" />
<item android:drawable="@drawable/btndefault" />
</selector>
然后在layout文件中使用:
<Button
...
android:background="@drawable/mbutton"
...
注意:btnfocused、btnfocusedpressed、btnpressed、btndefault这几个是button不同状态下的背景图片,放在drawable或其他几个drawable文件夹中。
效果(随便拣的几张图片):(本来有几张图片没有上传上来)
default:
focused:
pressed:
focused&pressed:
这里还有个问题,focused在pressed后消失,所以没有获取到focusedpressed样式。