The theme of this share comes from the problems encountered in the actual development of the author. The specific phenomenon is as follows: When the ordinary 9-patch graph is used as the backGround property of the TextView, the whole TextView has a certain Padding value. However, I do not give the padding property, and even in the preview view, the padding effect is not shown. But when it runs, it somehow has an inside margin. Let’s start with the layout code:

<? The XML version = "1.0" encoding = "utf-8"? > <TextView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_gravity="center" android:background="@mipmap/background" android:text="@string/app_name" tools:context=".MainActivity"> </TextView>Copy the code

A preview of the interface in Android Studio looks like this:



The image with red border is the background, which is a.9 PNG file processed by 9-patch.

Obviously, it’s normal to preview without margins, so let’s run it. The effect is shown below:



As you can see, when it’s actually run, the inside margin appears.

PNG file. Open this image in Android Studio and find that its zoom Settings look like this:



So you can see that the dark pink part in the center is the free stretch part. What defines it is the black lines around the image.

Next, we check “Show Content”, that is, preview the display content area, as shown below:



As you can see, the content is only displayed in the central non-white space, which is what we end up seeing in action.

Now that the root of the problem has been identified, how can we solve it? Quite simply, when defining an image stretch, we should use the top and left edges to underline the definition, while the right and bottom edges define the area where the content can be filled. Therefore, make the following changes:



If you look closely at the upper and lower edges, the left and right edges, you can see that the upper and lower edges are not completely crossed, but define the stretch part; The bottom and right sides are fully underlined, limiting the area where the content can be filled.

Run again and see that the inner margin has disappeared.



There is, of course, another solution. In the XML layout file, force the padding of the TextView to be 0dp and the padding to be 0. However, I prefer to use the former method, because the forced 0 method is often difficult to handle when the padding value is really needed.

Well, the above is the development tips to share with you, I hope to help you.