读书人

selector的运用

发布时间: 2012-08-25 10:06:20 作者: rapoo

selector的使用
使用selector可以实现在XML中动态改变布局:
(1)在res/drawable下定义一个文件advancedbutton.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/onfocusimage" /><item android:state_focused="true" android:state_pressed="true"android:drawable="@drawable/clickimage" /><item android:state_focused="false" android:state_pressed="true"android:drawable="@drawable/clickimage" /><item android:drawable="@drawable/lostfocusimage" /></selector>

(2)在布局文件main.xml中使用selector
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent" android:background="#FF25CCDD"><ImageButton android:id="@+id/image_Button"android:layout_width="fill_parent" android:layout_height="wrap_content"android:background="@drawable/advancedbutton"/><Button android:text="Button01" android:id="@+id/Button01"android:layout_width="wrap_content" android:layout_height="wrap_content"></Button></LinearLayout>具体示例见附件

读书人网 >移动开发

热点推荐