The difference and usage of padding and Layout_margin in Android

Source: Internet
Author: User

Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka

First, the definition

Android:layout_margin is the extra space to set the upper and lower left and right borders of the view

Android:padding is the distance that sets the border of the content relative to the view

padding, meaning "fill", like padding with a padded pad, a padding of a control and padding inside this control, this shows padding is that the defined control A is the parent control, and the interior content is spaced from control a. The Layout_margin is the control of the A control, which is the parent control, and is the space between a and a.

In fact, the concept is very simple, padding is standing in the perspective of the parent view to describe the problem, it specifies that its contents must be the distance from the parent view boundary. Margin is to stand in their own perspective to describe the problem, and other (up and down) the distance between the view, if there is only one view at the same level, then its effect is basically the same as padding


Such as

When the buttons set the above two properties separately, the effect is different. android:paddingleft= "30px" button set content (example) off the left edge of the button 30 pixels android:layout_marginleft= "30px" The entire button is set from the left side of the content 30 pixels


ii. Examples of use

1, two are not added

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android: background= "#ffffff" >    <textview        android:layout_width= "100dip"        android:layout_height= "100dip"         android:background= "#000000"        android:text= "Hello"/></relativelayout>
Effect:



2, only add padding

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "     android:padding = "100dip"    android:background= "#ffffff" >    <textview        android:layout_width= "100dip"        android: layout_height= "100dip"         android:background= "#000000"        android:text= "Hello"/></relativelayout>

Effect:


3. Only add margin

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android: background= "#ffffff" >    <textview        android:layout_width= "100dip"        android:layout_height= "100dip"         android:background= "#000000"         android:layout_margin= "30dip"        android:text= "Hello"/></ Relativelayout>
Effect:


Third, note the instructions

in LinearLayout, Relativelayout, Tablelayout, these 2 properties are all set to be valid.

In Framelayout, Android:layout_margin is invalid because the elements inside the framelayout are drawn from the upper left corner.

In Absolutelayout, there is no android:layout_margin attribute

In this reminder, the XML parameter contains layout of the parameter item as defined by the association of the control relative to the parent, there is no general definition of itself, the above content corresponds to this. Also similar to gravity and layout_gravity, with layout is relative to the parent's general position, but not with its own internal content of the general position.

Lin Bingwen Evankaka original works. Reprint please specify the source Http://blog.csdn.net/evankaka

The difference and usage of padding and Layout_margin in Android

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.