android之ImageButton实现选中改变背景,附加(图片自动适配组件大小)
下面是代码
<ImageButton android:id="@+id/jmxw" android:layout_width="fill_parent" android:layout_marginLeft="4dip" android:layout_height="0dip" android:layout_weight="784" android:background="@drawable/menupagexw"/>
2.在androidd项目的drawable下添加"android xml file"资源,选择类型为"selector",文件名为"menupagexw"
3.在menupagexw.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/jmxw_hl"/> <item android:state_pressed="false" android:drawable="@drawable/jmxw"/> </selector>
这样这个按钮在选中不和选中的情况下,背景就不一样了。
附加:
android:src=“图片id”,这时候图片不会根据组件大小而调整,但是android:background="图片id",会自动调整图片显示比列,来适配组件
原文:http://www.myexception.cn/image/1596451.html