ImageButton选择器在Android中不起作用

时间:2016-06-13 14:04:25

标签: android android-imagebutton

我在LinearLayout中有两个图像按钮,其中包含以下内容:

<ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imgImport"
        android:layout_margin="6dp"
        android:clickable="true"
        android:background="@drawable/bgselector2"
        android:src="@drawable/player_import" />

<ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imgEdit"
        android:layout_margin="6dp"
        android:clickable="true"
        android:src="@drawable/players"
        android:background="@drawable/bgselector" />

第二个图像按钮上的选择器正常工作但第一个图像按钮与下面的简单选择器不相同!那是为什么?

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

1 个答案:

答案 0 :(得分:0)

尝试为第二个状态添加android:state_pressed =“false”:

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

或许您应该将透明作为默认项目?

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