Android ApiDemos example resolution (123): Views-& gt; ImageButton

Source: Internet
Author: User

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
 

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.