ImageButton is a subclass of ImageView. Therefore, an image can be displayed. It also has the Button function, which can be pressed and responded to user click events. ImageButton is displayed in the same style as the Button by default. You can use android: src in the Layout file or setImageResource (int) in the code to specify an image for the Button.
ImageButton can reset the background image for ImageButton. You can also specify different states of the Button (get focus, lose focus, and press). For example, the green image is used as the default display, and the yellow image is displayed when the Button is pressed, orange is displayed when the focus is obtained. A simple method is to use the "selector" drawable resource, for example:
[Html]
<? 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_pressed"/> <! -Pressed->
<Item android: state_focused = "true"
Android: drawable = "@ drawable/button_focused"/> <! -Focused->
<Item android: drawable = "@ drawable/button_normal"/> <! -Default->
</Selector>
<? 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_pressed"/> <! -Pressed->
<Item android: state_focused = "true"
Android: drawable = "@ drawable/button_focused"/> <! -Focused->
<Item android: drawable = "@ drawable/button_normal"/> <! -Default->
</Selector> store the resource files in the/res/drawable directory and specify resources for ImageButton through android: src. The order of resource definition is very important.
In this example, the image resources of the Android system are used to specify the image for the three buttons:
[Html]
<ImageButton
Android: layout_width = "100dip"
Android: layout_height = "50dip"
Android: src = "@ android: drawable/sym_action_call"/>
<ImageButton
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ android: drawable/sym_action_chat"/>
<ImageButton
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ android: drawable/sym_action_email"/>
<ImageButton
Android: layout_width = "100dip"
Android: layout_height = "50dip"
Android: src = "@ android: drawable/sym_action_call"/>
<ImageButton
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ android: drawable/sym_action_chat"/>
<ImageButton
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ android: drawable/sym_action_email"/>
Author: mapdigit