视图的边距和填充之间的区别是什么?


当前回答

简单地说:

填充-在视图的边界内创建空间。 边框-创建视图边框之外的空间。

其他回答

填充是边框内的空间,在边框和实际视图内容之间。注意,填充完全围绕着内容:顶部、底部、右侧和左侧都有填充(可以是独立的)。

边距是边界之外的空间,在边界和视图旁边的其他元素之间。在图像中,边缘是整个物体外部的灰色区域。请注意,与填充一样,页边距完全围绕内容:在顶部、底部、右侧和左侧都有页边距。

一张图片说了超过1000个单词(从Margin Vs Padding - CSS属性中提取):

为了帮助我记住填充物的含义,我想到了一件有很多厚棉质填充物的大外套。我在我的外套里,但是我和我的棉袄在一起。我们是一个整体。

但为了记住空白,我会想,“嘿,给我一些空白!”那是我和你之间的空白。不要进入我的舒适区——我的底线。

为了让它更清楚,这里是一个图片的填充和空白在TextView:

上图的XML布局

<?xml version="1.0" encoding="utf-8"?>
<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:textColor="#000000"
        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:textColor="#000000"
        android:text="TextView margin only"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#c5e1b0"
        android:padding="10dp"
        android:textColor="#000000"
        android:text="TextView padding only"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#f6c0c0"
        android:padding="10dp"
        android:textColor="#000000"
        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:textColor="#000000"
        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:textColor="#000000"
        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="#c5e1b0"
        android:textColor="#000000"
        android:text="TextView no padding no margin"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#f6c0c0"
        android:textColor="#000000"
        android:text="TextView no padding no margin"
        android:textSize="20sp" />

</LinearLayout>

相关的

重力vs布局重力 Match_parent vs wrap_content

填充 填充在视图内部。例如,如果你给android:paddingLeft=20dp,那么视图内的项目将以20dp宽度从左排列。你也可以使用paddingRight, paddingBottom, paddingTop分别从右边,底部和顶部给出填充。

保证金 边距在视图之外。例如,如果你给android:marginLeft=20dp,那么视图将从左起20dp后排列。

填充在视图内部。

边距在视图之外。

这种差异可能与背景或大小属性有关。

下面的图片将让你了解填充和空白-