安卓ImageView选择器不起作用。

3
这是我的选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@drawable/white_small_down_arrow_v4"   android:state_pressed="true"/>  
    <item android:drawable="@drawable/white_small_up_arrow_v4" android:state_focused="false"/> 
    <item android:drawable="@drawable/white_small_up_arrow_v4" /> <!-- default -->

</selector>

这是我在 ImageView 上应用它的方法:

     <ImageView
            android:id="@+id/change_city_small_down_ImageView"
            android:clickable="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/changeCityRelativeLayout"
            android:layout_marginLeft="5dp"
            android:background="@drawable/change_city_selector"
   </ImageView>

现在的问题是,当我点击ImageView时,对应的状态drawable图像不会改变。我已经在其他小部件上尝试过了,也不起作用。我想不出为什么,因为我之前是用同样的方法做的,而且它是有效的。
当ImageView被点击时,我监视了它的状态。
v.hasFocus():false,v.isClickable():true,v.isInTouchMode():true,v.isEnabled():true,v.isPressed():true
我犯了一个可怕的错误,white_small_down_arrow_v4和white_small_up_arrow_v4实际上指向同一个方向,换句话说,它们是相同的图片。
所以,如果发现选择器无法正常工作,首先要检查状态可绘制是否相同...我的错误可能会帮助其他人。

这是ImageView附加在其上的布局: <RelativeLayout android:id="@+id/head" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="@drawable/flymeal_login_header_bg" android:gravity="center_vertical" <RelativeLayout/> - Qin
尝试使用android:src="@drawable/change_city_selector"代替android:background="@drawable/change_city_selector"。@Qin - TheFlash
@Indiandroid不起作用,我拼命尝试了各种状态组合。 - Qin
6个回答

2
尝试这样做:使用图像android:src="@drawable/change_city_selector"代替android:background="@drawable/change_city_selector"
<ImageView
        android:id="@+id/change_city_small_down_ImageView"
        android:clickable="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/changeCityRelativeLayout"
        android:layout_marginLeft="5dp"
        android:src="@drawable/change_city_selector"
</ImageView>

你是不是想说:android:state_active="true",因为我找不到state_activated属性。我复制了你的建议,但ImageView没有改变。 - Qin
你在 ImageView 中把 android:background="@drawable/change_city_selector" 改成了 android:src="@drawable/change_city_selector" 了吗? - adnanyousafch

1
尝试在您的ImageView属性中添加android:focusable="true"android:focusableintouchmode="true"

我尝试添加focusable和focusableintouchmode =“true”,但是没有起作用。 我还添加了onClick =“true”,但也没有起作用。不管怎样,谢谢。 - Qin

1

你需要在ImageView中将clickable设置为true

android:clickable="true"  

0

将您的选择更改为以下内容:

<?xml version="1.0" encoding="utf-8"?>
  <selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/white_small_down_arrow_v4"   android:state_pressed="true"/>  
    <item android:drawable="@drawable/white_small_up_arrow_v4" android:state_focused="true"/> 
    <item android:drawable="@drawable/white_small_up_arrow_v4" /> <!-- default -->
  </selector>

两者都应该是真的。

0

请确保您的选择器实际上具有state_selected而不是state_checked,后者将无法工作

your_selector.xml
    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_heart_selected" android:state_selected="true" />
    <item android:drawable="@drawable/ic_heart_unselected" android:state_selected="false" />
</selector>

如果你使用state_checked而不是state_selected,ImageView将永远不会工作。

0

试试这个,已经在 Android 4.4.2 和 5.1 上测试通过:

/drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <color android:color="@color/item_pressed"/>
    </item>
    <item>
        <color android:color="@android:color/transparent"/>
    </item>
</selector>

/布局

<ImageView
    android:id="@+id/ivOpenFile"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_margin="8dp"
    android:layout_alignParentRight="true"
    android:padding="4dp"
    android:background="@drawable/selector_settings_item"
    android:clickable="true"
    android:focusableInTouchMode="true"
    android:visibility="invisible"
    />

/java

ivOpenFile = (ImageView) rootView.findViewById(R.id.ivOpenFile);
ivOpenFile.setImageDrawable(VectorDrawableCompat.create(
        getResources(),
        R.drawable.vd_action_files_black,
        null));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接