详解 layout_marginTop 与 layout_marginBottom

本文详细解析了RelativeLayout中的layout_marginTop、layout_marginBottom、layout_marginLeft及layout_marginRight等属性的使用方法,并通过实例演示了这些属性如何影响控件的布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

layout_marginTop,layout_marginBottom,layout_marginRight,layout_marginLeft 是 RelativeLayout 中的四种属性,今天在进行UI设计的时候,着实困扰了好久,索性做个总结。

先上结论:

  • layout_marginTop 指定该属性所在控件距上部最近控件的最小值;

  • layout_marginBottom 指定该属性所在控件距下部最近控件的最小值;

  • layout_marginLeft 指定该属性所在控件距左边最近控件的最小值;

  • layout_marginRight 指定该属性所在控件距右边最近控件的最小值。

首先,需要明确的是,RelativeLayout 是相对布局,这意味着其中的所有控件如果不进行具体的位置确定,都将汇集在左上角。

下面,以代码为例,来学习这四个属性。
新建一个 project 为:RelativeLayoutTest,其他一切按照默认,一路 Enter 。接下来,我们只需要修改布局文件,然后查看效果。

一个控件的情况

layout_marginTop

  • 根据这个名字,我们也可以知道,它指的是 该属性所在控件的边缘与上部控件的边缘的距离,这里的边缘是最靠近的边缘,即两个控件之间的最小距离,这里是该控件的上部边缘与父控件的下部边缘。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <Button android:id="@+id/project"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:text="项目" 
      android:layout_marginTop="50dp"/>
</RelativeLayout>

我们在布局文件中新建了一个 Button 按钮,指定 layout_marginTop 的值为 50dp,效果如下:

这里写图片描述

按钮上边缘距离父控件的距离为 50dp。

layout_marginBottom

下面,我们将上部代码的 最后一行改为 android:layout_marginBottom="50dp",查看效果:

这里写图片描述

为什么按钮不是距离父控件的下部50dp,而是紧贴父控件的左上角呢?我们前面讲过,RelativeLayout 布局的控件是默认汇集在左上角的,我们指定的layout_marginBottom 的值远小于父控件默认的距离,此时,控件会按照默认取值。
layout_marginLeft 和 layout_marginRight 也是同理。

两个控件的情况

layout_marginTop

我们新增了一个“任务”按钮:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <Button android:id="@+id/project"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:text="项目" />
  <Button android:id="@+id/task"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:text="任务" 
      android:layout_marginTop="50dp"/>
</RelativeLayout>

查看效果如下:

这里写图片描述
可以很明显的看出,’任务‘按钮本来是与“项目”按钮重合的,现在,由于 android:layout_marginTop 的作用,它移到了上部边缘距离父布局下部边缘 50dp 的位置。

下面,我们给 “任务” 按钮添加一行代码:
android:layout_below="@id/project"
这行代码指定 “任务” 按钮位于 “项目” 按钮的下方,其他代码不变,我们来看看效果:

这里写图片描述

对比一下第一张图的项目按钮与本张图的任务按钮的位置,我们就可以发现,此时,layout_marginTop 指定的50dp 指的是:“任务”按钮与“项目”按钮之间的距离是50dp,而不是任务按钮与父控件下边缘的距离为50dp。

  • 现在,你应该明白我所说的最近控件的含义了吧。

layout_marginBottom

我们将上述代码中的 android:layout_marginTop="50dp" 改为 android:layout_marginBottom="50dp",并删除android:layout_below="@id/project",查看效果:

这里写图片描述

此时只有一个 “任务” 按钮了。因为,父布局是 RelativeLayout,两个按钮重合了。这也说明,当layout_marginBottom指定的距离小于默认的距离时,会按照默认的方式排列控件。

额外赠送一个: layout_margin

layout_margin 指定该属性所在的控件距上下左右最近控件的最小值。

仍旧是看一下代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <Button android:id="@+id/project"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:text="项目"
      android:layout_margin="10dp"/>
  <Button android:id="@+id/task"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:text="任务" 
      android:layout_margin="50dp"/>
</RelativeLayout>

查看效果如下:

这里写图片描述

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg" tools:context=".MainActivity"> <ImageView android:layout_width="match_parent" android:layout_height="85dp" android:src="@drawable/login" android:layout_marginTop="80dp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="MyMUSIC" android:layout_marginTop="20dp" android:textColor="#E62C21" android:gravity="center|center_vertical|center_horizontal" android:layout_marginBottom="65dp" android:textSize="25sp" android:textStyle="bold" /> <EditText android:id="@+id/login_account" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#B3FFFFFF" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:layout_marginTop="10dp" android:hint="请输入账号" android:textSize="25sp" android:textStyle="bold" android:textColorHint="#FFFFFF" android:background="@drawable/edit_login_bg" android:padding="10dp" android:drawablePadding="10dp" android:drawableLeft="@drawable/icon_login" /> <EditText android:id="@+id/login_pwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#B3FFFFFF" android:layout_marginTop="20dp" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:hint="请输入密码" android:textSize="25sp" android:textStyle="bold" android:textColorHint="#FFFFFF" android:background="@drawable/edit_login_bg" android:padding="10dp" android:drawablePadding="10dp" android:drawableLeft="@drawable/icon_pass" android:inputType="textPassword" /> <LinearLayout android:gravity="center_horizontal|center_vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:layout_marginLeft="50dp" android:layout_marginRight="50dp"> <Button android:id="@+id/login_denglu_btn" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="登录" android:layout_marginRight="20dp" android:background="@drawable/button_login_bg" android:textColor="#FDFDFD" android:foreground="?android:attr/selectableItemBackground" /> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="注册" android:layout_marginLeft="20dp" android:background="@drawable/button_login_bg" android:textColor="#FDFDFD" android:foreground="?android:attr/selectableItemBackground" />
03-16
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值