当我触摸ImageView时,为什么应用于ImageView的state-list-drawable不使用drawable for pressed-state?

时间:2016-04-22 02:43:52

标签: android imageview android-drawable xml-drawable statelistdrawable

我在XML中定义了ImageView,如下所示:

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_imageView"
        android:src="@drawable/ic_photo"
        android:background="@drawable/button_state_list_drawable" />

button_state_list_drawable.xml是:

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

    <item android:drawable="@drawable/button_pressed_state_drawable"
        android:state_pressed="true" />

    <item android:drawable="@drawable/button_enabled_state_drawable"
        android:state_enabled="true" />

</selector>

button_pressed_state_drawable.xml是:

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

    <size android:width="50dp"
        android:height="50dp" />

    <padding android:left="25dp"
        android:right="25dp"
        android:top="25dp"
        android:bottom="25dp" />

    <stroke android:color="#fff"
        android:width="1dp" />

    <solid android:color="#1aafd0" />

</shape>

button_enabled_state_drawable.xml是:

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

    <size android:width="50dp"
        android:height="50dp" />

    <padding android:left="25dp"
        android:right="25dp"
        android:top="25dp"
        android:bottom="25dp" />

    <stroke android:color="#fff"
        android:width="1dp" />

    <solid android:color="@android:color/transparent" />

</shape>

问题是当我点击/触摸ImageView时,button_pressed_state_drawable似乎没有被使用,即我看不到<solid android:color="#1aafd0" />在行动(这基本上是两个drawables之间的唯一区别),即背景颜色仍然是透明的而不是变为蓝色。我搞砸了哪里?

2 个答案:

答案 0 :(得分:4)

我认为您的ImageView不是clickable

尝试使用ImageView.setClicked(true)点击它。

        android:clickable="true"

同时确保您的image resource不在您的背景之列。

这就是我所做的工作正常。

    <ImageView
        android:id="@+id/fragment_imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_state_list_drawable"
        android:clickable="true"
        android:padding="20dp"
        android:src="@drawable/ic_launcher"/>

答案 1 :(得分:0)

<item android:drawable="@drawable/button_pressed_state_drawable"
    android:state_pressed="true" />

<item android:drawable="@drawable/button_enabled_state_drawable"
    android:state_enabled="true" />

按钮可以同时按下&#34;并且&#34;启用。&#34;尝试删除android:state_enabled =&#34; true&#34;从第2项开始。