`
Clareshao
  • 浏览: 22660 次
  • 性别: Icon_minigender_1
  • 来自: 无锡
社区版块
存档分类
最新评论

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>
具体示例见附件
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics