
我有一个带有图像src和背景颜色的ImageVIEw.
此图像是布局,即grIDvIEw项布局.
我想创建一个xml选择器,当选择项目时,图像背景会改变,但图像src不会改变.
类似于androID的主菜单与图标与文本,我想只突出图像.
我不会为对象的每个状态制作图像,我只想更改背景.
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" > <ImageVIEw androID:ID="@+ID/img_0" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParenttop="true" androID:layout_centerHorizontal="true" androID:layout_margintop="30dp" androID:background="@drawable/selector_categorIE" androID:src="@drawable/ic_launcher"/> <TextVIEw androID:ID="@+ID/text_1" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_below="@+ID/img_0" androID:layout_centerHorizontal="true" androID:layout_margintop="15dp" androID:gravity="center" androID:textAppearance="?androID:attr/textAppearanceLarge" /> <TextVIEw androID:ID="@+ID/text_2" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_below="@+ID/text_1" androID:layout_margintop="15dp" androID:gravity="center" androID:textAppearance="?androID:attr/textAppearanceMedium" /> <TextVIEw androID:ID="@+ID/text_3" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_below="@+ID/text_2" androID:layout_margintop="15dp" androID:gravity="center" androID:textAppearance="?androID:attr/textAppearanceMedium" /></relativeLayout>解决方法:
这可以通过StateListDrawable来实现.
创建背景可绘制资源,例如item /background.xml,在/ drawable文件夹中,包含如下内容:
<?xml version="1.0" enCoding="utf-8"?><selector xmlns:androID="http://schemas.androID.com/apk/res/androID"> <item androID:drawable="@color/color1" androID:state_pressed="true"/> <item androID:drawable="@color/color2" androID:state_selected="true"/> <item androID:drawable="@color/color3" androID:state_activated="true"/> <item androID:drawable="@color/color4"/></selector>然后在/values/colors.xml文件中提供颜色值:
<?xml version="1.0" enCoding="utf-8"?><resources> <color name="color1">#DDff8800</color> <color name="color2">...</color> <!-- more colors... --></resources>最后,使用androID:background =“@ drawable / item_background”将可绘制资源设置为布局中的项目背景.
src图像保持不变.
总结以上是内存溢出为你收集整理的android – 如何让imageview的选择器只改变背景但不改变图像源?全部内容,希望文章能够帮你解决android – 如何让imageview的选择器只改变背景但不改变图像源?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)