Android basic 8 -- ImageButton class

Source: Internet
Author: User

This class is mainly used to display an image button that can be clicked by users. Android: src and setImageResource () of the XML element of <ImageButton> can be used to specify the image to be displayed by the button. By default, it is a normal button, so you can set the background color to transparent, only display the image you want to display, not the background. To indicate different button states (Focus, selection, etc.), you can define different images for various States. For example, define a blue image as the default image, a yellow image as the image displayed when the current focal point is obtained, and a yellow image as the image displayed when the button is pressed. This can be done in a simple way-by configuring "selector. XML" in xml, for example:

<span style="font-family:Microsoft YaHei; font-size:14px"><span style="white-space:pre">    </span><selector xmlns:android="http://schemas.android.com/apk/res/android">      <span style="white-space:pre">  </span><item android:state_pressed="true"          <span style="white-space:pre">  </span>android:drawable="@drawable/btn_pressed" />  <span style="white-space:pre">      </span><item android:drawable="@drawable/btn_normal"/></span>  

 

<span style="font-family:Microsoft YaHei; font-size:14px"><span style="white-space:pre">    </span></selector></span>  

 

Save the preceding XML file to the res/drawable/folder and set the file name as a parameter to the android: src attribute of ImageButton (Note: for example, the xml file name is myselector. in xml format, set it to "@ drawable/myselector" and set android: background, but the effect is not the same ). Android will automatically search for the corresponding image in XML for Display Based on the button status change. <Item> the order of elements is very important, because it is used to determine whether it is applicable to the current button status, which is why the specified image in the normal (default) status is placed at the end, it is used only after the expression fails.

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.