Android - different image for rollover on ImageButton

社会主义新天地 提交于 2019-12-06 01:46:11

问题


Is it possible to specify a different image when the user's focus comes to an ImageButton? I want to display an image button on a LinearLayout and change the image when the user's focus comes on the button or when the user presses the button.

Thanks.


回答1:


Yes, you can do this. What you need is a drawable xml file that defines a selector.

<selector xmlns:android...
  <item android:state_enabled="false" android:state_focused="true" android:drawable="..." />
  <item android:state_enabled="true" android:state_focused="false" android:drawable="..." />
</selector>

Then, use the id of this drawable XML when specifying the ImageButton in your layout XML.




回答2:


The precedent answer did not work for me. Here is the code I found somewhere else:

    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
         <item android:drawable="@drawable/refresh_pushed" android:state_pressed="true" />
         <item android:drawable="@drawable/refresh" />
    </selector>

You can also add a state for foccussed objects by adding a line and using:

android:state_focused="true"



来源:https://stackoverflow.com/questions/606694/android-different-image-for-rollover-on-imagebutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!