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>
具体示例见附件
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页