The difference between padding and margin in Android

Source: Internet
Author: User

Difference

The padding is inside the control

Margin is the outside of the control

, the Red line area is margin, the black area is padding

The following are the effects of TextView's various Padding,margin

It's all wrap_content.

<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"  Android:layout_width="Match_parent"android:layout_height="Match_parent"  Android:orientation="vertical" >                <textview  android:layout _width  = "wrap_content"  android:layout_height  = "wrap_content"  android:layout_margin  = "10DP"  android:background  = "#c5e1b0"  android:text  = " TextView margin only " android:textsize  =" 20SP " />     <textview  android:layout _width  = "wrap_content"  android:layout_height  = "wrap_content"  android:layout_margin  = "10DP"  android:background  = "#f6c0c0"  android:text  = " TextView margin only " android:textsize  =" 20SP " />     <textview  android:layout _width  = "wrap_content"  android:layout_height  = "wrap_content"  android:background  =< Span class= "Hljs-value" > "#c5e1b0"  android:padding  =" 10DP " android:text  =" TextView padding only " android:textsize  =" 20SP " />     <textview  android:layout _width  = "wrap_content"  android:layout_height  = "wrap_content"  android:background  =< Span class= "Hljs-value" > "#f6c0c0"  android:padding  =" 10DP " android:text  =" TextView padding only " android:textsize  =" 20SP " />     <textview  android:layout _width  = "wrap_content"  android:layout_height  = "wrap_content"  android:layout_margin  = "10DP"  android:background  = "#c5e1b0"  android:padding  =         "10DP"  android:text  = "TextView padding and margin"  
     
      android:textsize 
      = "20sp" />     <textview  android:layout _width  = "wrap_content"  android:layout_height  = "wrap_content"  android:layout_margin  = "10DP"  android:background  = "#f6c0c0"  android:padding  =         "10DP"  android:text  = "TextView padding and margin"  
     
      android:textsize 
      = "20sp" />     <textview  android:layout _width  = "wrap_content"  android:layout_height  = "wrap_content"  android:background  =< Span class= "Hljs-value" > "#c5e1b0"  android:text  =" TextView no padding no margin " android:textsize  =" 20SP "/>     <TextViewandroid:layout_width="Wrap_content"android:layout_height= "Wrap_content" android:background="#f6c0c0"android:text="TextView no padding no margin"  Android:textsize="20sp" />                                        </linearlayout>

Wide and high fixed size

<linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  android:layout_width  =" match_parent " android:layout_height  =" match_parent " android:orientation  =;     <textview  android:layout _width  = "350DP"  android:layout_height  = "50DP"  android:layout_margin  =" 10DP " android:background  =" # C5e1b0 " android:text  =" TextView margin only "
       android:textsize  = "20sp" />     <textview  android:layout _width  = "350DP"  android:layout_height  = "50DP"  android:layout_margin  =" 10DP " android:background  =" # F6c0c0 " android:text  =" TextView margin only "
       android:textsize  = "20sp" />     <textview  android:layout _width  = "350DP"  android:layout_height  = "50DP"  android:background  =" #c5e1b0 " android:padding  =" 10DP        " android:text  =" TextView padding only " android:textsize  = "20sp" />     <textview  android:layout _width  = "350DP"  android:layout_height  = "50DP"  android:background  =" #f6c0c0 " android:padding  =" 10DP        " android:text  =" TextView padding only " android:textsize  = "20sp" />     <textview  android:layout _width  = "350DP"  android:layout_height  = "50DP"  android:layout_margin  =" 10DP " android:background  =" # C5e1b0 " android:padding  =" 10DP " android:text  = "TextView padding and margin"  android:textsize  = "20sp" />     <textview  android:layout _width  = "350DP"  android:layout_height  = "50DP"  android:layout_margin  =" 10DP " android:background  =" # F6c0c0 " android:padding  =" 10DP " android:text  = "TextView padding and margin"  android:textsize  = "20sp" />     <textview  android:layout _width  = "350DP"  android:layout_height  = "50DP"  android:background  =" #c5e1b0 " android:text  =" TextView no padding no margin " android:textsize  = "20sp" />     <TextViewandroid:layout_width= "350dp"android:layout_height=" 50DP "android:background=" #f6c0c0 "android:text=" TextView no padding no margin "  android:textsize="20sp" />                                        </linearlayout>

Contrast effects, and code. The difference should be seen by everyone.

The difference between padding and margin in Android

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.