android:padding和android:layout_margin的区别

margin和padding是隔开元素中最常用的两个属性

◆Padding属性:

Padding属性用来描述元素的边框和它的子元素之间插入多少空间,它分为上(padding-top)右(padding-right)下(padding-bottom)左(padding-left)和一个快捷方式padding

Margin属性:

Margin属性用来描述元素的边框和包含它的父元素的边框之间插入多少空间,和padding属性类似,它也分为上(margin-top)右(margin-right)下(margin-bottom)左(margin-left)和一个快捷方式margin

Android:layout_margin就是设置view的上下左右边框的额外空间

android:padding是设置内容相对view的边框的距离

在LinearLayout、RelativeLayout、TableLayout中,这2个属性都是设置都是有效的

在FrameLayout中,android:layout_margin是无效的,因为FrameLayout里面的元素都是从左上角开始绘制的

在AbsoluteLayout中,没有android:layout_margin属性

 android:layout_alignParentRight="true" 属性是子控件针对父容器的。 且父容器必须是RelativeLayout。

之前一直没有搞懂Android:padding和android:layout_margin的区别,其实概念很简单,padding是站在父view的角度描述问题,它规定它里面的内容必须与这个父view边界的距离。margin则是站在自己的角度描述问题,规定自己和其他(上下左右)的view之间的距离,如果同一级只有一个view,那么它的效果基本上就和padding一样了。例如我的XML layout代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="10dip"
    android:paddingRight="10dip"
    android:paddingTop="10dip"
    android:paddingBottom="10dip"
    >
<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#FF0000"
    android:text="@string/hello"
    android:paddingLeft="50dip"
    android:paddingRight="50dip"
    android:paddingTop="50dip"
    android:paddingBottom="50dip"
	android:layout_marginBottom="10dip"
    />
    <TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#FF0000"
    android:text="@string/hello"
    android:paddingLeft="50dip"
    android:paddingRight="50dip"
    android:paddingTop="50dip"
    android:paddingBottom="50dip"
 	android:layout_marginBottom="10dip"
    />
    <TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#FF0000"
    android:text="@string/hello"
    android:paddingLeft="50dip"
    android:paddingRight="50dip"
    android:paddingTop="50dip"
    android:paddingBottom="50dip"
    android:layout_marginBottom="10dip"
    />
    <TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#FF0000"
    android:text="@string/hello"
    android:paddingLeft="50dip"
    android:paddingRight="50dip"
    android:paddingTop="50dip"
    android:paddingBottom="50dip"
    android:layout_marginBottom="10dip"
    />
</LinearLayout>

  

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="10dip"
    android:paddingRight="10dip"
    android:paddingTop="10dip"
    android:paddingBottom="10dip"
    >
<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#FF0000"
    android:text="@string/hello"
    android:paddingLeft="50dip"
    android:paddingRight="50dip"
    android:paddingTop="50dip"
    android:paddingBottom="50dip"
	android:layout_marginBottom="10dip"
    />
    <TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#FF0000"
    android:text="@string/hello"
    android:paddingLeft="50dip"
    android:paddingRight="50dip"
    android:paddingTop="50dip"
    android:paddingBottom="50dip"
 	android:layout_marginBottom="10dip"
    />
    <TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#FF0000"
    android:text="@string/hello"
    android:paddingLeft="50dip"
    android:paddingRight="50dip"
    android:paddingTop="50dip"
    android:paddingBottom="50dip"
    android:layout_marginBottom="10dip"
    />
    <TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#FF0000"
    android:text="@string/hello"
    android:paddingLeft="50dip"
    android:paddingRight="50dip"
    android:paddingTop="50dip"
    android:paddingBottom="50dip"
    android:layout_marginBottom="10dip"
    />
</LinearLayout>

  

 那么我会得到如下的效果,图上已经很明确的标出来区别咯。

padding和margin的区别

原文地址:https://www.cnblogs.com/nuliac/p/5802658.html