1.新建2个android xml文件,根元素选择shape.
正常情况,shape1代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@drawable/clr_normal"/>
<corners android:radius="8dp"/>
</shape>
按下时,shape2代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@drawable/clr_pressed"/>
<corners android:radius="8dp"/>
</shape>
以上代码其实等于定于两个按钮
2.新建一个android xml文件,根元素选择selector.命名mine1
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/my_shape" android:state_pressed="false"/>
<item android:drawable="@drawable/my_shape1" android:state_pressed="true"/>
</selector>
以上代码等于定义不同状态显示不同的按钮
3.在MainActivity的页面文件当中,将按钮的background属性定义为mine1
<Button
android:id="@+id/bt1"
android:layout_width="242dp"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:background="@drawable/mine1"
android:text="查看学习时间"
android:textColor="@drawable/white" />