先看效果
//只需要把button的背景设置为一个样式
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="test"
android:textColor="#FFFFFF"
android:layout_marginTop="20dip"
android:background="@drawable/a_bg"
android:gravity="center"/>
再在drawable下新建一个selector类型的xml文件 命名为a_bg.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/a_bg_1" android:state_pressed="true"/>
<item android:drawable="@drawable/a_bg_1" android:state_focused="true"/>
<item android:drawable="@drawable/a_bg_2"/>
</selector>
这里a_bg_1是按下的状态
a_bg_2是正常状态
然后新建shape类型的a_bg_1.xml和a_bg_2.xml
分别是
a_bg_1.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#cccccc" />//设置按钮正常状态的颜色
<stroke
android:width="1dip"
android:color="#00000088" />//设置边框颜色 后面aa为透明度
<corners
android:bottomLeftRadius="25dp"
android:bottomRightRadius="25dp"
android:topLeftRadius="25dp"
android:topRightRadius="25dp" />//设置4个方位边角的半径
</shape>
a_bg_2.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#222222" />//设置按钮正常状态的颜色
<stroke
android:width="3dip"
android:color="#000000aa" />//设置边框颜色 后面aa为透明度
<corners
android:bottomLeftRadius="25dp"
android:bottomRightRadius="25dp"
android:topLeftRadius="25dp"
android:topRightRadius="25dp" />//设置4个方位边角的半径
</shape>