简体   繁体   English

按下状态时,Android imageButton不会更改颜色

[英]Android imagebutton not change color when state pressed

I have in my layout a button, which is has to change image when pressed. 我的布局中有一个按钮,按下该按钮时必须更改图像。 I Have a selector xml. 我有一个选择器xml。 Here is button: 这是按钮:

<ImageButton
            android:id="@+id/cwcfwdvcs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:clickable="true"
            android:layout_marginTop="16dp"
            android:background="@drawable/button_add_to_favorites_unactive"
            android:drawable="@drawable/add_to_favorites"
            android:text="Favotites"/>

Here is selector add_to_favorites.xml: 这是选择器add_to_favorites.xml:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/button_add_to_favorites_active"
      android:state_checked="true" />
<item android:drawable="@drawable/button_add_to_favorites_unactive" />

You should use the button selector state like the following code ie(res/drawable/button.xml) 您应该像下面的代码一样使用按钮选择器状态,即((res / drawable / button.xml)

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

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

</item>

<item android:state_focused="true" android:drawable="@drawable/button_add_to_favorites_active">

</item>

<item android:drawable="@drawable/button_add_to_favorites_active">        

</item>
</selector>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM