layout_margin & padding

来源:互联网 发布:交换机压力测试软件 编辑:程序博客网 时间:2024/05/22 12:35

layout_margin 和 padding两个属性,是我们常用的,但是很多新手会混淆这两个属性的差别。我们下面就说一下这两个具体的功能和区别。

首先从命名上我们看到margin是有前缀layout_,这个前缀就代表了这个属性不会作用于控件本身的,而是它会相对与外部控件或者布局才会起作用,而padding的命名方式没有前缀layout_,这种命名方式就说明这个属性是作用于控件自身的。在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:gravity="center"    android:orientation="vertical">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@android:color/holo_red_dark"        android:textColor="@android:color/white"        android:text="FragmentA"/>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@android:color/holo_red_dark"        android:textColor="@android:color/white"        android:text="FragmentA"/></LinearLayout>

效果图如下:

这里写图片描述

如图所示,这是两个紧紧挨着的TextView。接下来,我们用这两个TextView来测试layout_margin 和 padding属性。

layout_margin 外边距

在给第一个TextView设置layout_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:gravity="center"    android:orientation="vertical">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@android:color/holo_red_dark"        android:textColor="@android:color/white"        android:layout_margin="10dp"        android:text="FragmentA"/>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@android:color/holo_red_dark"        android:textColor="@android:color/white"        android:text="FragmentA"/></LinearLayout>

效果:

这里写图片描述

图解:

这里写图片描述

通过上面的图解是不是很容易理解layout_margin了呢?

layout_margin是在上下左右都分别增加对应数值宽度的外边距,它不会印象控件本身内容,而会作用域相邻的控件位置。

layout_marginBottom
layout_marginTop
layout_marginLeft
layout_marginRight

这四个属性同时使用的效果就是layout_margin的效果。

padding 内边距

<?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:gravity="center"    android:orientation="vertical">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@android:color/holo_red_dark"        android:textColor="@android:color/white"        android:padding="10dp"        android:text="FragmentA"/>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@android:color/holo_red_dark"        android:textColor="@android:color/white"        android:text="FragmentA"/></LinearLayout>

效果:

这里写图片描述

效果很明显,文本框的范围变大了,其原理与layout_margin类似,图解如下:

这里写图片描述

内部红色框代表文本框原来的大小即为添加padding属性时的大小,外面大的边框为文本框添加padding后的大小。

我们根据上面代码的运行效果图可以看出padding是作用于控件(这里只TextView)本身的,当增加padding之后文本框本身变大,从背景颜色的延伸可以看出。

padding 与layout_margin相同,是对控件本身的上下左右都增加相对数值的宽度。

paddingLeft
paddingRight
paddingTop
paddingBottom

上面四个属性共同使用的效果则为padding的效果。

上面是自己的一些总结。如有描述不当的地方希望大家支出!

2 0