Cri*_*ian 574

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

边距是边框之外,边框和此视图旁边的其他元素之间的空格.在图像中,边距是整个对象外部的灰色区域.请注意,与填充一样,边距完全围绕内容:顶部,底部,右侧和左侧都有边距.

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

替代文字

  • 答案是HTML/CSS,问题是Android.Android的视图模型受到HTML的启发,但并不完全相同.首先,边界不是那里的第一类大型物体. (53认同)
  • 注意:在Android中,layout_width属性包括内容和填充.(在HTML中,css width属性仅指内容宽度.)正如Seva所说,Android没有内置的边框概念.您可以使用9-patch png背景或xml矢量drawable在Android中添加边框. (47认同)
  • 值得注意的是,背景是基于边距修改而不是填充(在Android中). (11认同)

Sur*_*gch 548

为了帮助我记住填充物的含义,我想到了一件带有大量厚棉垫的大衣.我穿着外套,但是我和我的衬垫在一起.我们是一个单位.

但要记住保证金,我想," 嘿,给我一些余地! "这是我和你之间的空白.不要进入我的舒适区 - 我的保证金.

为了更清楚,这里是一个填充和边距的图片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>
Run Code Online (Sandbox Code Playgroud)

有关


Flo*_*ern 72

填充在视图内.

保证金在View之外.

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


Sev*_*yev 41

填充在视图内,边距在外面.填充适用于所有视图.根据视图,填充和边距之间可能存在或可能不存在视觉差异.

例如,对于按钮,特征按钮背景图像包括填充,但不包括边距.换句话说,添加更多填充使按钮看起来更大,而添加更多边距只会使按钮和下一个控件之间的间隙变宽.

TextView另一方面,对于s,填充和边距的视觉效果是相同的.

边距是否可用取决于视图的容器,而不是视图本身.在LinearLayout支持保证金,AbsoluteLayout(现在已经过时) - 没有.


Aks*_*wal 23

下面的图片将让您了解填充和边距 -

在此输入图像描述


AnK*_*nKr 8

填充表示小部件和小部件原始框架之间的空间.但边距是小部件的原始帧与边界其他小部件的帧之间的空间.在此输入图像描述.


raj*_*aja 7

填充是边框与实际图像或单元格内容之间边界内的空间.边距是边框之外,边框与此对象旁边的其他元素之间的空格.


Akl*_*kli 7

有时你只能通过填充或边距来实现相同的结果.示例:

假设视图X包含视图Y(又名:视图Y在视图X内).

- 使用Margin = 30查看Y或使用Padding = 30查看X将获得相同的结果:视图Y的偏移量为30.


小智 7

填充
的填充是View.For示例的内部,如果你给android:paddingLeft=20dp,那么该视图内的物品将与安排20dp从left.You宽度也可以使用paddingRight,paddingBottom,paddingTop它们分别以得到由右,底部和顶部填充.

保证金
保证金在a View.之外.例如,如果您给出android:marginLeft=20dp,则视图20dp将从左侧开始排列.