Android通过selector改变界面状态
2010-07-01 14:41:00 来源:WEB开发网先上图,看看效果:
初始效果
按下后效果
在res/drawable文件夹新增一个文件,此文件设置了图片的触发状态,你可以设置 state_pressed,state_checked,state_pressed,state_selected,state_focused,state_enabled 等几个状态:
Java代码
< selector xmlns:android="http://schemas.android.com/apk/res/android">
< item android:state_pressed="true" android:drawable="@drawable/arrow_pressed" />
< item android:drawable="@drawable/arrow_normal" />
< item android:state_checked="true" android:drawable="@drawable/arrow_pressed" />
< /selector>
< selector xmlns:android="http://schemas.android.com/apk/res/android">
< item android:state_pressed="true" android:drawable="@drawable/arrow_pressed" />
< item android:drawable="@drawable/arrow_normal" />
< item android:state_checked="true" android:drawable="@drawable/arrow_pressed" />
< /selector>
实现如下,注意其中的android:src="@drawable/imageselector"
Xml代码
< ImageView android:layout_width="wrap_content"
android:layout_height="@android:dimen/app_icon_size"
android:layout_alignParentRight="true" android:scaleType="fitCenter"
android:layout_gravity="center" android:src="@drawable/imageselector"
android:clickable="true" android:focusable="true" android:id="@+id/blacklistImageView"
android:layout_marginRight="10dip" />
< ImageView android:layout_width="wrap_content"
android:layout_height="@android:dimen/app_icon_size"
android:layout_alignParentRight="true" android:scaleType="fitCenter"
更多精彩
赞助商链接