Buttion状态变化背景跟着变化
应用中经常可以看到,按钮什么的在被按下的状态时背景会变化。自定义一些属性就可以达到效果。
Android提供了灵活的绘制选择机制,可根据视图状态转变视图外观。每个状态通过一个单独的部分而存在。例如:在正常、禁用、按下、高亮状态下的按钮有着不同的背景颜色。请看下面的代码示例:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/pink_btn_bg"
android:state_pressed="false" />
<item android:drawable="@drawable/gray_btn_bg"
android:state_pressed="true" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="10dp"/>
<gradient android:angle="270" android:startColor="#E496C0" android:endColor="#D681B2"/>
竖向颜色变化
</shape>
gray_btn_bg
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#9a9d9d" />
<corners android:radius="10dp" />
</shape>
android:background="@drawable/button_state"