目的:创建按钮,当按钮使按钮有不同的形状状态
在res下drawable-hdpi保存按钮初始状态left.png和按钮按下后的状态图片rigth.png.
将图片文件拖到创建好的文件夹drawable-hdpi中
创建一个一般的XML文件,新建图形状态组合步骤为
drawable_change → New → Other... → XML → XML File
注意:将Left_Rigth.XML 改成 left_right.XML
在Left_Right.XML填入以下代码
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 按的状态 -->
<item
android:state_pressed="true"
android:drawable="@drawable/left"/>
<!-- 默认的状态 -->
<item
android:drawable="@drawable/rigth"/>
</selector>
在 activity_main.XML加以下代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test.MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="@+id/button1"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:background="@drawable/left_right"/>
<Button
android:id="@+id/button2"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_below="@id/button1"
android:background="@drawable/left_right"/>
</RelativeLayout>
测试结果如下:
转载请注明出处:http://blog.youkuaiyun.com/u010499449/article/details/42192383