2017-05-10 51 views
0

我有一個ImageView,我想讓它可點擊來按下它並更改圖像。我用下面的:ImageVIew與狀態爲棒棒糖版本不起作用?

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_enabled="true" android:drawable="@drawable/back"/> 
<item android:state_pressed="true" android:drawable="@drawable/back_pressed"/> 
</selector> 

上SrcCompat我把上面的,但沒有..

我如何使用XML選擇做呢?

+0

「對於棒棒糖版本無法正常工作」....和較低的API? – Opiatefuchs

+0

並且您需要爲默認狀態或錯誤狀態添加drawable ... – Opiatefuchs

回答

0

對於選擇不使用未啓用,只使用僅按下或激活

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@drawable/back" /> //for Default dont check state 
<item android:drawable="@drawable/back_pressed" android:state_pressed="true" /> 
<item android:drawable="@drawable/back_pressed" android:state_selected="true" /> 

0

您需要在ImageView XML添加屬性android:clickable="true"

<ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="?attr/selectableItemBackgroundBorderless" 
       android:clickable="true" 
       android:src="@mipmap/your_drawable" /> 

創建繪製

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@drawable/back" /> //for Default dont check state 
<item android:drawable="@drawable/back_pressed" android:state_pressed="true" /> 
<item android:drawable="@drawable/back_pressed" android:state_selected="true" /> 
相關問題