Android uses NinePatch images for variable-Size buttons

Source: Internet
Author: User

In some Android applications, you sometimes need to use an extended image as the background. The method is to use NinePatch.

The following is an example of using a NinePatch image to set the background for a Button to implement an image Button that can change with the text size:

Upload a ninepatchresource image (button.9.png), detailed method reference (http://www.bkjia.com/kf/201205/133933.html );
Drag button.9.png (drag) to the/res/drawable-mdpi directory of the android project.
Modify the main. XML file:
[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
 
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"/>
 
<Button
Android: id = "@ + id/btn1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/button"
Android: text = "music"
Android: textSize = "12sp"/>
 
<Button
Android: id = "@ + id/btn2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/button"
Android: text = "dialer"
Android: textSize = "24sp"/>
 
<Button
Android: id = "@ + id/btn3"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/button"
Android: text = "wallpaper"
Android: textSize = "48sp"/>
 
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">

<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"/>

<Button
Android: id = "@ + id/btn1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/button"
Android: text = "music"
Android: textSize = "12sp"/>

<Button
Android: id = "@ + id/btn2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/button"
Android: text = "dialer"
Android: textSize = "24sp"/>

<Button
Android: id = "@ + id/btn3"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/button"
Android: text = "wallpaper"
Android: textSize = "48sp"/>

</LinearLayout>

Put the Button component on the UI and set the text and size on the Button. Use the "android: background" attribute to set the background of the Button to "@ drawable/button", that is, "The buttonimage in the drawable-mdpi/directory". The androidframe will go to the button.9.png file. Because button.9.png is a NinePatch image, it will be extended with the text size on the Button.

At this time, all the work has been completed and no code needs to be rewritten. The program running effect is as follows:








From Young's column

Related Article

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.