一、线性布局

LinearLayout又称为线性布局,是一种非常常用的布局。这个布局会将它包含的控件在线性方向上依次排列。我们可以通过指定它的orientation属性来决定它是垂直方向排列还是水平方向上排列。

举例1:垂直方向排列


当排列方向是vertical,内部控件就不能将高度指定为match_parent
## 举例2:水平方向排列

如果LinearLayout的排列方向是horizontal,内部控件就绝对不能将宽度指定为match_parent。

举例3:layout_gravity属性


这个属性指定控件在布局中的对齐方式。跟gravity属性用法相似,但是需要注意的是当排列方向是水平的时候,只有垂直方向上的对齐方式才会生效,因为此时水平方向上的长度是不固定

举例4:layout_weight属性


这个属性可以让控件按比例分配屏幕。我们将EditText和Button的宽度都指定为0dp,不用担心显示问题。现在控件的宽度由android:layout_weight属性决定,我们将2个控件的值都设置为1就表示这两个控件平分屏幕

相对布局

RelativeLayout又称为相对布局。它可以通过相对定位的方式让控件出现在布局任何位置。也正因为如此,这个布局的属性非常多。

举例1:基于控件布局

  1. //按键在左上角
  2. <Button
  3. android:id="@+id/button1"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:layout_alignParentLeft="true"
  7. android:layout_alignParentTop="true"
  8. android:text="Button1"
  9. />
  10. //按键在右上角
  11. <Button
  12. android:id="@+id/button2"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_alignParentRight="true"
  16. android:layout_alignParentTop="true"
  17. android:text="Button2"
  18. />
  19. //按键在中间
  20. <Button
  21. android:id="@+id/button3"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_centerInParent="true"
  25. android:text="Button3"
  26. />
  27. //按键在左下角
  28. <Button
  29. android:id="@+id/button4"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:layout_alignParentLeft="true"
  33. android:layout_alignParentBottom="true"
  34. android:text="Button4"
  35. />
  36. //按键在右下角
  37. <Button
  38. android:id="@+id/button5"
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. android:layout_alignParentRight="true"
  42. android:layout_alignParentBottom="true"
  43. android:text="Button5"
  44. />

举例2:基于控件布局

  1. //Button3处于布局中央
  2. <Button
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:layout_centerInParent="true"
  6. android:id="@+id/button3"
  7. android:text="Button3"/>
  8. //Button3右上角
  9. <Button
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:id="@+id/button1"
  13. android:text="Button1"
  14. android:layout_above="@id/button3"
  15. android:layout_toRightOf="@id/button3"/>
  16. //Button3左上角
  17. <Button
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:id="@+id/button2"
  21. android:text="Button2"
  22. android:layout_above="@id/button3"
  23. android:layout_toLeftOf="@id/button3"/>
  24. //Button3右下角
  25. <Button
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:id="@+id/button4"
  29. android:text="Button4"
  30. android:layout_below="@id/button3"
  31. android:layout_toRightOf="@id/button3"/>
  32. //Button3左下角
  33. <Button
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:id="@+id/button5"
  37. android:text="Button5"
  38. android:layout_below="@id/button3"
  39. android:layout_toLeftOf="@id/button3"/>

三、帧布局

FrameLayout又称为帧布局。这种布局没有方便的定位方式,所有的控件都会默认摆放在布局的左上角

四、百分比布局

这种布局允许直接指定控件在布局中所占的百分比,这样可以轻松实现按任意比例分割布局的效果
这种布局被Android团队定义在support库当中,我们需要在项目的build.gradle中添加百分比布局库的依赖,保证百分比布局在所有Android版本上的兼容性

输入完之后会跳出一个提示框

这个提示告诉我们,gradle文件自上次同步之后发生了变化,需要再次同步才能是项目正常工作。这里只需要点击Sync Now就可以了。

举例1:4个按键平分界面

  1. <android.support.percent.PercentFrameLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. >
  7. <Button
  8. android:id="@+id/button1"
  9. android:text="Button1"
  10. android:layout_gravity="left|top"
  11. app:layout_widthPercent="50%"
  12. app:layout_heightPercent="50%"
  13. />
  14. <Button
  15. android:id="@+id/button2"
  16. android:text="Button2"
  17. android:layout_gravity="right|top"
  18. app:layout_widthPercent="50%"
  19. app:layout_heightPercent="50%"
  20. />
  21. <Button
  22. android:id="@+id/button3"
  23. android:text="Button3"
  24. android:layout_gravity="left|bottom"
  25. app:layout_widthPercent="50%"
  26. app:layout_heightPercent="50%"
  27. />
  28. <Button
  29. android:id="@+id/button4"
  30. android:text="Button4"
  31. android:layout_gravity="right|bottom"
  32. app:layout_widthPercent="50%"
  33. app:layout_heightPercent="50%"
  34. />
  35. </android.support.percent.PercentFrameLayout>

附件列表

详解Android基本布局的更多相关文章

  1. 详解Android首选项框架ListPreference

    详解Android首选项框架ListPreference 原文地址 探索首选项框架 在深入探讨Android的首选项框架之前,首先构想一个需要使用首选项的场景,然后分析如何实现这一场景.假设你正在编写 ...

  2. (转载)实例详解Android快速开发工具类总结

    实例详解Android快速开发工具类总结 作者:LiJinlun 字体:[增加 减小] 类型:转载 时间:2016-01-24我要评论 这篇文章主要介绍了实例详解Android快速开发工具类总结的相关 ...

  3. 详解android:scaleType属性

    详解android:scaleType属性 转自:http://blog.csdn.net/encienqi/article/details/7913262    http://juliaailse. ...

  4. adb shell 命令详解,android

    http://www.miui.com/article-275-1.html http://noobjava.iteye.com/blog/1914348 adb shell 命令详解,android ...

  5. 详解Android Activity---启动模式

    相关的基本概念: 1.任务栈(Task)   若干个Activity的集合的栈表示一个Task.   栈不仅仅只包含自身程序的Activity,它也可以跨应用包含其他应用的Activity,这样有利于 ...

  6. Android Binder IPC详解-Android学习之旅(96)

    linux内存空间与BInder Driver Android进程和linux进程一样,他们只运行在进程固有的虚拟空间中.一个4GB的虚拟地址空间,其中3GB是用户空间,1GB是内核空间 ,用户空间是 ...

  7. 详解Android中的四大组件之一:Activity详解

    activity的生命周期 activity的四种状态 running:正在运行,处于活动状态,用户可以点击屏幕,是将activity处于栈顶的状态. paused:暂停,处于失去焦点的时候,处于pa ...

  8. adb shell 命令详解,android, adb logcat

    http://www.miui.com/article-275-1.html http://noobjava.iteye.com/blog/1914348 adb shell 命令详解,android ...

  9. 图文详解 Android Binder跨进程通信机制 原理

    图文详解 Android Binder跨进程通信机制 原理 目录 目录 1. Binder到底是什么? 中文即 粘合剂,意思为粘合了两个不同的进程 网上有很多对Binder的定义,但都说不清楚:Bin ...

随机推荐

  1. 20165230 《Java程序设计》实验四 Android程序设计实验报告

    20165230 <Java程序设计>实验四 Android程序设计实验报告 一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:田坤烨 学号:20165230 成绩: 指导 ...

  2. Windows版Oracle重建EM---备注

    前提条件添加环境变量 ORACLE_HOSTNAME=<主机名:如:DESKTOP-P6J1a>ORACLE_SID=orclORACLE_UNQNAME=orcl 执行删除命令 C:\U ...

  3. 五、springboot单元测试

    1.为什么要写测试用例 1. 可以避免测试点的遗漏,为了更好的进行测试,可以提高测试效率 2. 可以自动测试,可以在项目打包前进行测试校验 3. 可以及时发现因为修改代码导致新的问题的出现,并及时解决 ...

  4. docker安装(2018-03-14版本)

    [安装] 说明一: CENTOS或RHEL自带的docker源不一定是最新的,所以必须到docker.com去找到最新的yum源进行安装 说明二: docker的安装方式有两种: 1. 从指定网站获取 ...

  5. vue总结07 常用插件

    插件 开发插件 插件通常会为 Vue 添加全局功能.插件的范围没有限制——一般有下面几种: 添加全局方法或者属性,如: vue-custom-element 添加全局资源:指令/过滤器/过渡等,如 v ...

  6. opencv(1)图像处理

    2.图像操作 图片裁剪 裁剪是利用array自身的下标截取实现 HSV空间 除了区域,图像本身的属性操作也非常多,比如可以通过HSV空间对色调和明暗进行调节.HSV空间是由美国的图形学专家A. R. ...

  7. 插入标识列identity_insert

    插入标识列identity_insert 在进行数据插入时,如果插入列名包括标识列,常常会遇到以下3种提示: 一.“当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'xxxxxxxx ...

  8. 日志、字段备注查询、自增ID联系设置、常用存储过程

    -----获取数据字典SQL(表字段说明)SELECT     [Table Name] = OBJECT_NAME(c.object_id),     [Column Name] = c.name, ...

  9. SQL SERVER 触发器介绍

    什么是触发器 触发器对表进行插入.更新.删除的时候会自动执行的特殊存储过程.触发器一般用在check约束更加复杂的约束上面.触发器和普通的存储过程的区别是:触发器是当对某一个表进行操作.诸如:upda ...

  10. 使用mockito模拟静态方法

    一.为什么要使用Mock工具 在做单元测试的时候,我们会发现我们要测试的方法会引用很多外部依赖的对象,比如:(发送邮件,网络通讯,远程服务, 文件系统等等). 而我们没法控制这些外部依赖的对象,为了解 ...