我怎样才能让选择一个ImageView的,只改变背景而不是图像源? [英] How can I make selector for an imageview to change only the background but not the image source?

查看:109
本文介绍了我怎样才能让选择一个ImageView的,只改变背景而不是图像源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个图像src和背景色设置一个ImageView的。

此图片到版式是GridView的项目布局。

我想创建一个XML选择,当该项目是choosen,图像背景的变化,但图像的src没有。

类似于与文本的Andr​​oid图标的主菜单中的东西,我想强调只有图像。

我不会做的对象的每个状态的图像,我只是想改变背景。

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>    < ImageView的
        机器人:ID =@ + ID / img_0
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:layout_centerHorizo​​ntal =真
        机器人:layout_marginTop =30dp
        机器人:背景=@绘制/ selector_categorie
        机器人:SRC =@绘制/ ic_launcher/>    <的TextView
        机器人:ID =@ + ID /文本1
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / img_0
        机器人:layout_centerHorizo​​ntal =真
        机器人:layout_marginTop =15dp
        机器人:比重=中心
        机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?    <的TextView
        机器人:ID =@ + ID / _2
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID /文本1
        机器人:layout_marginTop =15dp
        机器人:比重=中心
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>    <的TextView
        机器人:ID =@ + ID / text_3
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / _2
        机器人:layout_marginTop =15dp
        机器人:比重=中心
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>
< / RelativeLayout的>


解决方案

这可以用的 StateListDrawable

创建背景绘制资源,如 item_background.xml ,在 /绘制文件夹,包含像这样:

 <?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
  <项目机器人:可绘制=@颜色/颜色1机器人:STATE_ pressed =真/>
  <项目机器人:可绘制=@颜色/颜色2机器人:state_selected =真/>
  <项目机器人:可绘制=@色/ COLOR3机器人:state_activated =真/>
  <项目机器人:可绘制=@色/ color4/>
< /选择>

然后在你的 /values​​/colors.xml 文件中提供的颜色值:

 <?XML版本=1.0编码=UTF-8&GT?;
<资源>
  <颜色名称=颜色1>#DDff8800< /彩色>
  <颜色名称=颜色2> ...< /彩色>
  <! - 更多的颜色... - >
< /资源>

最后,设置绘制资源与项目背景布局的android:背景=@绘制/ item_background

的src 图像保持不变。

I have an ImageView with both image src and background color set.

This image is into a layout that is the gridview item layout.

I would to create an xml selector that when the item is choosen, the image background change, but the image src not.

Something similar to the main menu of android with icon with text, I want to highlight only the image.

I would not to make an image for each state of the object, I only want to change the background.

<?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>

解决方案

This can be achieved with a StateListDrawable.

Create a background drawable resource, e.g. item_background.xml, in your /drawable folder, containing something like this:

<?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>

Then provide the color values in your /values/colors.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <color name="color1">#DDff8800</color>
  <color name="color2">...</color>
  <!-- more colors... -->
</resources>

Finally, set that drawable resource as the item background in your layout with android:background="@drawable/item_background".

The src image remains untouched.

这篇关于我怎样才能让选择一个ImageView的,只改变背景而不是图像源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆