视图的边距和填充之间的区别是什么?
当前回答
下面的图片将让你了解填充和空白-
其他回答
除了上述所有正确答案之外,另一个区别是填充增加了视图的可点击区域,而边距则不会。如果你有一个小的可点击图像,但想让点击处理程序宽容,这是有用的。
例如,看到我的ImageView布局的这张图片(Android图标),我将paddingbutton设置为100dp(图像是股票启动器mipmap ic_launcher)。有了附加的点击处理程序,我能够点击外面和下面的图像,仍然注册一个点击。
简单地说: 填充改变盒子的大小(用一些东西)。 边距改变不同框之间的间距
为了帮助我记住填充物的含义,我想到了一件有很多厚棉质填充物的大外套。我在我的外套里,但是我和我的棉袄在一起。我们是一个整体。
但为了记住空白,我会想,“嘿,给我一些空白!”那是我和你之间的空白。不要进入我的舒适区——我的底线。
为了让它更清楚,这里是一个图片的填充和空白在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后排列。
边距在视图内,边距在视图外。填充可用于所有视图。根据视图的不同,边距和边距之间可能存在或可能没有视觉差异。
例如,对于按钮,特征按钮背景图像包括填充,但不包括空白。换句话说,添加更多的填充会使按钮在视觉上看起来更大,而添加更多的边距只会使按钮和下一个控件之间的距离更宽。
另一方面,对于TextViews,填充和边缘的视觉效果是相同的。
页边距是否可用是由视图的容器决定的,而不是视图本身。在LinearLayout中支持边距,在AbsoluteLayout(现在已经过时了)中不支持边距。
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。