如何设置图像按钮和backgroundImage为不同的状态? [英] How to set image button backgroundimage for different state?
问题描述
我想ImageButton的两种状态一)正常II)的触摸(或点击)。
I want imagebutton with two states i) normal ii) touch(or click).
我已经将正常图片在的ImageButton 的背景和我想的更改的图片(pressed)从的onclick 的方法,但它并没有改变。
I have set normal image in the imagebutton background and I am trying to change image(pressed) from onclick method, but it doesn't change.
我想,如果我pressed图像按钮,然后形象应该从正常变成pssed,直到我$ P $ preSS等按钮,但它不会发生
任何人都可以建议我怎么能做到这一点是选择器或在运行时?
Can anyone suggest to me how can I do this with selector or at run time?
下面是我的imagebuttonpanel code。
Here is my imagebuttonpanel code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="bottom"
android:id="@+id/buttonpanel">
<ImageButton android:id="@+id/buttonhome"
android:layout_width="80dp"
android:layout_height="36dp"
android:focusable="true"
android:background="@drawable/homeselector">
</ImageButton>
<ImageButton android:id="@+id/buttonsearch"
android:layout_height="36dp"
android:layout_width="80dp"
android:background="@drawable/searchselector"
android:focusable="true">
</ImageButton>>
<ImageButton android:id="@+id/buttonreg"
android:layout_height="36dp"
android:layout_width="80dp"
android:background="@drawable/registerselector"
android:focusable="true">
</ImageButton>>
<ImageButton android:id="@+id/buttonlogin"
android:layout_height="36dp"
android:layout_width="80dp"
android:background="@drawable/loginselector"
android:focusable="true">
</ImageButton>
</LinearLayout>
和我的选择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/homehover"
/> <item android:state_focused="true" android:drawable="@drawable/homehover"
/> <item android:drawable="@drawable/home" /> <item android:state_window_focused="true"
android:drawable="@drawable/homehover" /> -->
<item android:state_enabled="false" android:drawable="@drawable/homehover" />
<item android:state_pressed="true" android:state_enabled="true"
android:drawable="@drawable/homehover" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="@drawable/homehover" />
<item android:state_enabled="true" android:drawable="@drawable/home" />
</selector>
和我也试图改变图像资源的ontouch和onclick事件,但它并不能帮助。
And I have also tried to change image resource on ontouch and onclick event, but it does not help.
推荐答案
建立在你绘制一个XML文件是这样的:
Create an xml file in your drawable like this :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="@drawable/btn_sendemail_disable" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/btn_send_email_click" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/btn_sendemail_roll" />
<item
android:state_enabled="true"
android:drawable="@drawable/btn_sendemail" />
</selector>
和相应的设置图像,然后将该XML作为背景的的ImageButton
。
And set images accordingly and then set this xml as background of your imageButton
.
这篇关于如何设置图像按钮和backgroundImage为不同的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!