我想在按下事件时更改 ImageButton 的背景颜色。
这是我所做的:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="@color/rose_normal" />
<solid
android:state_pressed="true"
android:color="@color/rose_fonce" />
<stroke
android:width="1sp"
android:color="@color/rose_fonce" />
</shape>
我的按钮在“rose_normal”颜色中很好,但在按下时从来没有在“rose_fonce”颜色中。
问题出在哪里 ?
编辑:问题已解决:
创建一个名为 background_rounded_button.xml 的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/rounded_button_rose_fonce" android:state_selected="true"></item>
<item android:drawable="@drawable/rounded_button_rose_fonce" android:state_pressed="true"></item>
<item android:drawable="@drawable/rounded_button_rose_normal"></item>
</selector>
rounded_button_rose_fonce.xml :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="@color/rose_fonce" />
<stroke
android:width="1sp"
android:color="@color/rose_fonce" />
</shape>
rounded_button_rose_normal.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="@color/rose_normal" />
<stroke
android:width="1sp"
android:color="@color/rose_fonce" />
</shape>
最后,为按钮应用背景:
<ImageButton
android:id="@+id/all_annonce_button_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/background_rounded_button.xml"
android:padding="16dp"
android:src="@drawable/ic_action_search" />