First: (two buttons in the figure, not two images,)
The first button is imagebutton, and the second button is button.
Imagebutton. XML Code
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout <br/> xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: Orientation = "vertical" <br/> <! -- You can set the image displayed on the button through Android: src = "@ drawable/icon" --> <br/> <imagebutton <br/> Android: id = "@ + ID/button_imagebutton" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> <button <br/> Android: Id = "@ + ID/button_button" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: Background = "@ drawable/sbtn5_2" <br/> Android: TEXT = "goggle" <br/> </button> <br/> </linearlayout> <br/>
The activity code is as follows:
Package cn.com. chenzheng_java; </P> <p> Import android. app. activity; <br/> Import android. content. res. resources; <br/> Import android. graphics. drawable. drawable; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. widget. imagebutton; <br/> Import android. widget. toast; <br/>/** <br/> * @ description imagebutton application test <br/> * @ author chenzheng_java <br/> * @ since 2011/03/09 <br/> */ <br/> public class imagebuttonactivity extends activity {</P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. imagebutton); <br/> imagebutton = (imagebutton) findviewbyid (R. id. button_imagebutton); </P> <p> // imagebutton. setbackgroundresource (R. drawable. excel); Method 1 </P> <p> // method 2 <br/> resources Resources = This. getresources (); <br/> drawable d = resources. getdrawable (R. drawable. excel); <br/> imagebutton. setbackgrounddrawable (d); </P> <p> imagebutton. setonclicklistener (new view. onclicklistener () {</P> <p> @ override <br/> Public void onclick (view v) {<br/> toast. maketext (imagebuttonactivity. this, "clicked", toast. length_long ). show (); </P> <p >}< br/>}); </P> <p >}< br/>
Others are default. Run the command to get the corresponding results.
-----------------------------------------------------------------
Imagebutton inherits from the button. Its main feature is that the content displayed above is not text, but an image.
In the layout file, we can set images for it through the Android: src attribute. In the code, we can set it through setbackgrounddrawable ()/setbackgroundresource ()/setbackgroundcolor.
It should be noted that imagebutton does not support placing some text on the image. If you want to implement this function, you should use the button like our second button, you can set the Android: Text and Android: Background attributes to work together.
-------------------------------------------------------------------------
Button advanced:
Do you often see that in many commercial applications, buttons are displayed differently when getting focus and losing focus? We can imagine that to achieve this effect, we only need to capture the corresponding events and then implement them dynamically in the code. But don't you think this is very troublesome? Is there a more convenient way for us to implement this application?
Yes!
Now we will introduce this method:
Create an XML file imagebutton2.xml In the Res/drawable/folder. The Code is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <selector xmlns: Android = "http://schemas.android.com/apk/res/android"> <br/> <item Android: state_window_focused = "false" Android: drawable = "@ drawable/Excel"/> <br/> <item Android: state_focused = "true" Android: state_pressed = "true" <br/> Android: drawable = "@ drawable/icon"/> <br/> <item Android: state_focused = "false" Android: state_pressed = "true" <br/> Android: drawable = "@ drawable/Baidu"/> <br/> <item android Oid: state_selected = "true" Android: drawable = "@ drawable/sbtn5_2"/> <br/> <item Android: state_focused = "true" Android: drawable = "@ drawable/icon"/> <br/> </selector> <br/> <! -- <Br/> <selector xmlns: Android = "http://schemas.android.com/apk/res/android"> <br/> <item Android: state_window_focused = "false" Android: drawable = "@ drawable/picture background without focus"/> <br/> <item Android: state_focused = "true" Android: state_pressed = "true" <br/> Android: drawable = "@ drawable/background image when you click in non-touch mode"/> <br/> <item Android: state_focused = "false" Android: state_pressed = "true" <br/> Android: drawable = "@ drawable/background image when clicked in touch mode"/> <br/> <item Android: state_selected = "true" Android: drawable = "@ drawable/selected image background"/> <br/> <item Android: state_focused = "true" Android: drawable = "@ drawable/background of the image when the focus is obtained"/> <br/> </selector> <br/> --> <br/>
Then declare the button and set the Android: Background = "@ drawable/imagebutton2" attribute for it. Then we can see the desired result.
(The test result here is that except for pressing, other settings do not work and the code is correct. The problem should be in the simulator. I don't have an Android phone, so I can't do it, try it by yourself)
Paste the online resource description here. I will solve the selector usage as soon as possible.
Suddenly I saw this introduction to selector in the imagebutton API. I will paste it here for you to see. We can see from the introduction that our usage is correct, but it is not clear what operations will trigger what events.