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