9-patch and padding in android

Source: Internet
Author: User

I encountered such a problem a few days ago.
Our application customer saw that there was a problem with the UI. It was originally a Square area and the result was a rectangle, with some deviations.
After that, I began to modify it. I felt that the UI problem was definitely not well designed. 95% was because the configuration in xml was wrong. This was my first instinct.
After looking for a morning, I found that there was no problem in xml. The definition was indeed a Square area, and there was no padding settings. So I went to the Code in the afternoon to check if I set padding in the code.
I don't think the average person will do this kind of thing in the code, but it really didn't find it. Khan!

So I started debugging, and there were no special settings in the code, and it crashed. Later I found the setBackgroundResource api somewhat strange.
Void android. view. View. setBackgroundDrawable (Drawable d) and setBackgroundDrawable are called.
[Java]
SetPaddingFromDrawable (d, padding );
What does this look like? setPaddingFromDrawable has
[Java]
Draw. getPadding (padding)
Debug found that the padding was changed after going through this step, and did not continue the in-depth investigation. The guess is related to the setting of android: background. With this setting removed, the padding is gone.
Later, the artist confirmed my conjecture that padding was written in the 9patch image.

So when you cannot find the padding settings in the xml and code, but the results have the padding effect, it should be a good thing for 9-patch.

Next, let's take a look at 9-patch.
Take the native Email as an example. You can find a. 9 image: attachment_bg_holo.9.png (packages/apps/Email/res/drawable-hdpi)
The sdk provides us with a tool to view 9-patch images. In the tools Folder
Android-sdk/tools $./draw9patch
Open the above picture:

The following Slide lines look awkward because I scaled down the tool page and they were squeezed together.
When you slide the patch scale, the preview on the right will also change as the size changes.
Next, let's talk about where padding is.
The middle picture is surrounded by a black line (DOT). 9.
The black part is divided into two parts. The bottom and right black lines (points) control the padding.
Check show lock and you will see the image area (that is, I have removed the four-week cycle)
So what are the upper, lower, and left padding numbers?
A small grid is a pixel.
The top, bottom, and left padding numbers are 2, 3, and 2, respectively. (remove the outermost lattice because they are not part of the picture)
Then I'm right. How can I check whether the sdk provides another tool in the tools Folder hierarchyviewer?
Android-sdk/tools $./draw9patch

See the value on the right.
The following is an official description.
Guide/topics/graphics/2d-graphics.html # nine-patch
You can also define an optional drawable section of the image (gradient tively, the padding lines) by drawing a line on the right and bottom lines. if a View object sets the NinePatch as its background and then specifies the View's text, it will stretch itself so that all the text fits inside only the area designated by the right and bottom lines (if needed DED ). if the padding lines are not supported ded, Android uses the left and top lines to define this drawable area.

Next let's take a look at what the black lines above the 9-patch and on the left do.
Below are the pictures of http://developer.android.com/guide/topics/graphics/2d-graphics.html?nine-patch

I think you can understand that the other two lines control the stretch area, either one or more.
The following is an official description.
Guide/topics/graphics/2d-graphics.html # nine-patch writes
The border is used to define the stretchable and static areas of the image. you indicate a stretchable section by drawing one (or more) 1-pixel-wide black line (s) in the left and top part of the border (the other border pixels shoshould be fully transparent or white ). you can have as your stretchable sections as you want: their relative size stays the same, so the largest seest always remain the largest
Author: su1216

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.