【问题标题】:How can I make selector for an imageview to change only the background but not the image source?如何使图像视图的选择器仅更改背景而不更改图像源?
【发布时间】:2013-01-13 11:22:11
【问题描述】:

我有一个同时设置了图像源和背景颜色的 ImageView。

此图像位于 gridview 项目布局的布局中。

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

类似于带有文本图标的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>

【问题讨论】:

    标签: android xml imageview


    【解决方案1】:

    这可以通过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 图像保持不变。

    【讨论】:

    • 好的,但是如何禁用 gridview 按下状态的标准行为并使用 imageview 按下状态?
    • 如果你的GridView 只持有ImageViews,他们应该默认关注触摸。否则,在 ImageView 上设置 focusable 标志:developer.android.com/reference/android/view/…
    • 我属于第二种情况。如果我将焦点放在我无法点击,则不会调用 clicklistener。也许我应该只禁用网格的标准选择器。
    • 没错,如果你想区分点击ImageView和点击GridView中的一般项目,你必须为ImageView点击设置一个额外的监听器.如果您可以在问题中提供一些代码也会有所帮助,例如网格元素的项目布局如何。
    • 我会使用 gridview.setonitemclicklistener,但是当我触摸 gridview 元素时只突出显示图像。所以,我认为最好的选择是禁用gridview highlight 将其设置为#00000000 并将选择器添加到imageview。我做到了,但是当我触摸网格元素时,文本视图会改变颜色。为什么?
    猜你喜欢
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 2019-05-11
    • 1970-01-01
    相关资源
    最近更新 更多