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を押したときに、それに応じた状態の描画可能な画像が変化しないことです。他のwiddetでも試しましたが、うまくいきません。以前は同じ方法でこれを行っていたので、理由がわかりませんが、うまくいきます。

クリックされたときのイメージビューの状態を監視しました。

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 は実際には同じ方向を指しています。つまり、それらは同じ絵です。

したがって、セレクターが機能しないことがわかった場合、おそらく私の間違いは他の誰かを助けるでしょう。最初にすべきことは、状態のドローアブルが同じかどうかを確認することです....

4

5 に答える 5

1

ImageView プロパティにandroid:focusable="true"andを追加してみてください。android:focusableintouchmode="true"

于 2014-04-04T03:45:28.293 に答える
0

これを試してください。Android 4.4.2 および 5.1 でチェックされています。

/ドローアブル

<?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));
于 2016-09-07T14:11:41.947 に答える
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>

どちらも真実であるべき

于 2015-02-08T06:10:35.600 に答える