开发者

Making a button look pressed in

I'm using Eclipse to write the android application. I've added some standard buttons from the Form Widgets tab and successfully got them opening new windows which display additional butto开发者_Python百科ns.

I would like the button that was pressed, to change appearance and continue to look pressed in after it is selected.


create xml file using the button image like this

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

add the color folder in values

 <?xml version="1.0" encoding="utf-8"?>
 <resources>    
    <color name="transparent">#00000000</color>  
 </resources>


can use selector

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

now set this drawable on background property of button in XML, now in coding take a boolean flag when button is pressed set the flag and set the bacground of the button(selected image) and on again click reset flag value and change the imageBackground to the selector again, thats it!!!


Use ImageButton and change the background of the button. you can use this kind of images depends of you need. Like this https://web.archive.org/web/20160429124711/http://www.devahead.com/blog/2011/08/creating-a-custom-android-button-with-a-resizable-skin/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜