视图的margin和padding有什么区别?
视图的margin和padding有什么区别?
为了帮助我记住 padding 的含义,我想到了一件有许多 厚棉垫 的大衣。我和我的被填充的外套在一起,我们是一个整体。
但是要记住margin,我想到了“嘿,给我留点余地!”这是我和你之间的空白空间。不要进入我的舒适区——我的边距。
为了更清楚地说明,在下面的 TextView
中显示了 padding 和 margin 的图示:
<?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>
内边距(Padding)是指位于边框与视图实际内容之间的空间。请注意,内边距完全包围内容:上、下、左、右都有内边距(可以独立设置)。
外边距(Margin)是指位于边框以及相邻元素之间的空间。在图片中,外边距是整个对象外部的灰色区域。请注意,与内边距一样,外边距也完全包围内容:上、下、左、右都有外边距。
一张图片胜过千言万语(来自Margin Vs Padding - CSS Properties):
内边距(Padding)指的是视图(View)内部的空白区域。
外边距(Margin)指的是视图(View)外部的空白区域。
这种区别可能会影响背景或大小属性。
内边距(Padding)是指在视图内部的间隔,而外边距(Margin)则是指视图外面的间隔。所有的视图都支持内边距,但根据不同的视图,内边距和外边距之间可能有或没有视觉上的区别。
例如,在按钮中,特征按钮背景图包括了内边距,但不包括外边距。换句话说,增加内边距会使按钮看起来更大,而增加外边距仅会扩大按钮和下一个控件之间的间距。
然而,在TextView中,内边距和外边距的视觉效果是相同的。
外边距是否可用取决于视图的容器,而不是视图本身。在LinearLayout中支持外边距,在已经被认为过时的AbsoluteLayout中不支持外边距。
内边距
内边距是在一个视图内部的,例如如果你设置了android:paddingLeft=20dp
,那么视图内部的项目就会从左侧开始以20dp
的宽度排列。你也可以使用paddingRight
、paddingBottom
、paddingTop
来分别从右侧、底部和顶部设置内边距。
外边距
外边距是在视图外部的,例如如果你设置了android:marginLeft=20dp
,那么视图就会从左侧向后排列20dp
。
内边距是指边框与实际图像或单元格内容之间的空间。 外边距是指边框与该对象旁边的其他元素之间的空间。