视图的边距和填充之间的区别是什么?
当前回答
简单地说:
填充-在视图的边界内创建空间。 边框-创建视图边框之外的空间。
其他回答
填充用于在视图及其内容之间添加空白。
边距用于在不同视图之间添加空格。
对于边距和边距,我们有两种方法来设置,
两边都设为相等的值 根据需要设置边的具体值
价值相等的各方:
你可以使用android:padding="15dp"来设置15dp所有边的填充
android:layout_margin="15dp"用于设置15dp所有边距
支持具体价值观:
填充
android:paddingBottom设置底部边缘的填充 android:paddingStart设置填充在开始边缘意味着在视图的左侧 android:paddingEnd设置填充在结束边缘意味着在右侧的视图 android:paddingTop设置顶部边距
保证金
layout_marginBottom在这个视图的底部指定额外的空间。 android:layout_marginEnd在结束端指定额外的空间,意味着在这个视图的右边。 android:layout_marginStart在开始侧指定额外的空间,意味着在这个视图的左边。 android:layout_marginTop在这个视图的顶部指定额外的空间。
假设你在视图中有一个按钮视图的大小是200 * 200,按钮的大小是50 * 50,按钮标题是HT。现在,边距和填充的区别是,你可以在视图中设置按钮的边距,例如从左边20,从上面20,填充将调整文本在按钮或文本视图中的位置等。例如,填充值从左边20,所以它将调整文本的位置。
为了帮助我记住填充物的含义,我想到了一件有很多厚棉质填充物的大外套。我在我的外套里,但是我和我的棉袄在一起。我们是一个整体。
但为了记住空白,我会想,“嘿,给我一些空白!”那是我和你之间的空白。不要进入我的舒适区——我的底线。
为了让它更清楚,这里是一个图片的填充和空白在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
有时你可以通过使用填充或边距来达到同样的效果。例子:
假设视图X包含视图Y(也就是:视图Y在视图X内部)。
-View Y的Margin=30或View X的Padding=30将达到相同的结果:View Y的offset为30。
下面的图片将让你了解填充和空白-
推荐文章
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- 如何在新的材质主题中改变背面箭头的颜色?
- androidviewpager与底部点
- 相同的导航抽屉在不同的活动
- 用于桌面应用程序的Swing vs JavaFx
- Base64长度计算?
- 使用PyCrypto AES-256加密和解密
- 如何从视图中获得托管活动?
- 单一的TextView与多种颜色的文本
- 如何在非活动类(LocationManager)中使用getSystemService ?