当前位置: 首页 > ai >正文

【详解marginTop与marginBottom】

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

先上结论:

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

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

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

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

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

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

一个控件的情况

## layout_marginTop

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

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

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

## layout_marginBottom

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

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

两个控件的情况

layout_marginTop

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

 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <Button android:id="@+id/project"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="项目" />
  10. <Button android:id="@+id/task"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="任务"
  14. android:layout_marginTop="50dp"/>
  15. </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 指定该属性所在的控件距上下左右最近控件的最小值。

仍旧是看一下代码:

 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <Button android:id="@+id/project"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="项目"
  10. android:layout_margin="10dp"/>
  11. <Button android:id="@+id/task"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="任务"
  15. android:layout_margin="50dp"/>
  16. </RelativeLayout>

查看效果如下;



来自为知笔记(Wiz)


转载于:https://www.cnblogs.com/hultron/p/88841139c4ebbe76afa53ddf26098f64.html

http://www.xdnf.cn/news/11137.html

相关文章:

  • 计算机硬件基础知识
  • 计算机毕业设计——简单的网页设计
  • Google 各国地址
  • Keil uVision5 5.38官方下载、安装及注册教程
  • Connection reset原因分析和解决方案
  • 2019年7月总结
  • 【115】StrokeIt相关操作
  • jquery CDN(内容分发网络)使用
  • 【C语言】:巧用移位操作符,位操作符解决问题
  • 终端服务Terminal Service的几个使用技巧
  • 【详细介绍使用说明】echarts图表插件
  • sort和stable_sort的区别和联系
  • JAVA基于J2ME的手机游戏开发(毕设+实现+源码+数据库)
  • 【MATLAB】进阶绘图 ( 双 y 轴图形 | plotyy 函数 | Histogram 统计图形 | hist 函数 )
  • eclipse che安装教程
  • 11 年了,我在 CSDN 被访问 800 万+次! 被点赞 3.5 万+次,被收藏 9.6 万+次!
  • 安卓10拨号流程梳理
  • NRF2401无线通信
  • java编程指南100本电子书
  • 网络***实战——使用wirelesskeyview轻松获取无线密码
  • 《魔兽世界》10个让你感动流泪的故事
  • 开源计算机视觉库OpenCV详解
  • FPGA开源网站和论坛介绍
  • 6to4 自动隧道技术
  • c语言printf()输出格式大全
  • 计算机的组成
  • 纪中2016.10.6比赛不明总结
  • 关于Excel提示外部源的链接无法删除的解决方法(附强制解除办法)
  • 汇编语言快速入门(非常详细)
  • 悄悄蒙上你的眼睛 后门程序知识完全解析