常见的布局

视频建议采用超清模式观看, 欢迎点击订阅我的优酷

Android的图形用户界面是由多个View和ViewGroup构建出来的。View是通用的UI窗体小组件,比如按钮(Button)或者文本框(text field),而ViewGroup是不可见的,是用于定义子View布局方式的容器,比如网格部件(grid)和垂直列表部件(list)。

Android提供了一个对应于View和ViewGroup子类的一系列XMl标签,我们可以在XML里使用层级视图元素创建自己的UI。

我们常见的布局包括:

  • LinearLayout ( 线性布局 )
  • RelativeLayout ( 相对布局 )
  • FrameLayout ( 帧布局 )
  • GridLayout(网格布局 Android4.0 引入的)

不常见的布局:

  • Tablelayout(表格布局)
  • AbsoluteLayout(绝对布局 废弃了)

我们接下来重点讲下常见的布局,对于不常见的布局,大家了解就可以

LinearLayout

即一行展开或者一列展开,也可以嵌套

重要的属性 android:orentation 方向,包括Vertical(垂直) 和horizontal 水平

参考代码

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. tools:context=".MainActivity">
  7. <Button
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:text="button1" />
  11. <Button
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:text="button2" />
  15. <Button
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:text="button3" />
  19. <Button
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:text="button4" />
  23. <LinearLayout
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:orientation="horizontal">
  27. <!--layout_weight 比重-->
  28. <Button
  29. android:layout_width="0dp"
  30. android:layout_height="wrap_content"
  31. android:layout_weight="1"
  32. android:text="button1" />
  33. <Button
  34. android:layout_width="0dp"
  35. android:layout_height="wrap_content"
  36. android:layout_weight="1"
  37. android:text="button2" />
  38. <Button
  39. android:layout_width="0dp"
  40. android:layout_height="wrap_content"
  41. android:layout_weight="1"
  42. android:text="button3" />
  43. <Button
  44. android:layout_width="0dp"
  45. android:layout_height="wrap_content"
  46. android:layout_weight="1"
  47. android:text="button4" />
  48. </LinearLayout>
  49. </LinearLayout>

RelativeLayout

视频建议采用超清模式观看, 欢迎点击订阅我的优酷

控件显示都是相对于其它布局或者控件

常见的属性:

android:layout_centerHrizontal 水平居中

android:layout_centerVertical 垂直居中

android:layout_centerInparent 相对于父元素完全居中

android:layout_alignParentBottom 贴紧父元素的下边缘

android:layout_alignParentLeft 贴紧父元素的左边缘

android:layout_alignParentRight 贴紧父元素的右边缘

android:layout_alignParentTop 贴紧父元素的上边缘

android:layout_below 在某元素的下方

android:layout_above 在某元素的的上方

android:layout_toLeftOf 在某元素的左边

android:layout_toRightOf 在某元素的右边

android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐

android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐

android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐

android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐

当我们实现下图的界面, 采用相对布局要方便很多

参考代码:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5. <!--red green blue-->
  6. <TextView
  7. android:layout_alignParentTop="true"
  8. android:layout_alignParentLeft="true"
  9. android:layout_width="100dp"
  10. android:layout_height="100dp"
  11. android:background="#cc0000"
  12. android:text="red"
  13. android:gravity="center"
  14. />
  15. <TextView
  16. android:layout_alignParentTop="true"
  17. android:layout_alignParentRight="true"
  18. android:layout_width="100dp"
  19. android:layout_height="100dp"
  20. android:background="#ffff00"
  21. android:text="yellow"
  22. android:gravity="center"
  23. />
  24. <TextView
  25. android:layout_alignParentTop="true"
  26. android:layout_centerHorizontal="true"
  27. android:layout_width="100dp"
  28. android:layout_height="100dp"
  29. android:background="#ff1100"
  30. android:text="orange"
  31. android:gravity="center"
  32. />
  33. <TextView
  34. android:id="@+id/tv_center"
  35. android:layout_centerInParent="true"
  36. android:layout_width="100dp"
  37. android:layout_height="100dp"
  38. android:background="#ff1100"
  39. android:text="center"
  40. android:gravity="center"
  41. />
  42. <TextView
  43. android:layout_alignTop="@+id/tv_center"
  44. android:layout_toLeftOf="@+id/tv_center"
  45. android:layout_width="100dp"
  46. android:layout_marginRight="10dp"
  47. android:layout_height="100dp"
  48. android:background="#88ff00"
  49. android:text="left"
  50. android:gravity="center"
  51. />
  52. <TextView
  53. android:layout_alignTop="@+id/tv_center"
  54. android:layout_toRightOf="@+id/tv_center"
  55. android:layout_width="100dp"
  56. android:layout_height="100dp"
  57. android:background="#ff8800"
  58. android:layout_marginLeft="10dp"
  59. android:text="right"
  60. android:gravity="center"
  61. />
  62. <TextView
  63. android:layout_alignParentBottom="true"
  64. android:layout_width="match_parent"
  65. android:layout_height="100dp"
  66. android:background="#771122"
  67. android:text="bottom"
  68. android:gravity="center"
  69. />
  70. </RelativeLayout>

FrameLayout

视频建议采用超清模式观看, 欢迎点击订阅我的优酷

帧布局,即一层层叠起来,最先在xml总定义的都在最底下,入下图

需要注意的是 5.0以上的系统,按钮控件无论定义在前面还是后面,默认都是显示在最上面,不会被遮挡

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. >
  6. <!--5.0之前 后添加的控件在之前添加控件的上面 5.0之后 如果有按钮 按钮默认都是在最前面-->
  7. <TextView
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:text="button"
  11. />
  12. <ProgressBar
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:id="@+id/progressBar"
  16. android:layout_gravity="left|top" />
  17. </FrameLayout>

GridLayout

视频建议采用超清模式观看, 欢迎点击订阅我的优酷

网格布局

取代TableLayout

GridLayout优势可以避免Linearlayout多层嵌套

需要注意的是,GridLayout是Android4.0引入的布局,如果使用此布局,需要保证当前程序最低兼容版本14

首先它与LinearLayout布局一样,也分为水平和垂直两种方式,默认是水平布局,一个控件挨着一个控件从左到右依次排列。

但是通过指定android:columnCount设置列数的属性后,控件会自动换行进行排列。另一方面,对于GridLayout布局中的子控件,默认按照wrap_content的方式设置其显示,这只需要在GridLayout布局中显式声明即可。

其次,若要指定某控件显示在固定的行或列,只需设置该子控件的android:layout_row和android:layout_column属性即可,但是需要注意:android:layout_row=”0”表示从第一行开始,android:layout_column=”0”表示从第一列开始,这与编程语言中一维数组的赋值情况类似。

最后,如果需要设置某控件跨越多行或多列,只需将该子控件的android:layout_rowSpan或者layout_columnSpan属性设置为数值,再设置其layout_gravity属性为fill即可,前一个设置表明该控件跨越的行数或列数,后一个设置表明该控件填满所跨越的整行或整列。



这个界面建议大家使用GridLayout

参考代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:columnCount="4"
  6. android:orientation="horizontal"
  7. android:rowCount="5">
  8.   
  9. <Button
  10. android:id="@+id/one"
  11. android:text="1" />
  12.   
  13. <Button
  14. android:id="@+id/two"
  15. android:text="2" />
  16.    
  17. <Button
  18. android:id="@+id/three"
  19. android:text="3" />
  20. <Button
  21. android:id="@+id/devide"
  22. android:text="/" />
  23. <Button
  24. android:id="@+id/four"
  25. android:text="4" />
  26. <Button
  27. android:id="@+id/five"
  28. android:text="5" />
  29.   
  30. <Button
  31. android:id="@+id/six"
  32. android:text="6" />
  33. <Button
  34. android:id="@+id/multiply"
  35. android:text="×" />
  36. <Button
  37. android:id="@+id/seven"
  38. android:text="7" />
  39. <Button
  40. android:id="@+id/eight"
  41. android:text="8" />
  42. <Button
  43. android:id="@+id/nine"
  44. android:text="9" />
  45. <Button
  46. android:id="@+id/minus"
  47. android:text="-" />
  48. <Button
  49. android:id="@+id/zero"
  50. android:layout_columnSpan="2"
  51. android:layout_gravity="fill"
  52. android:text="0" />
  53.   
  54. <Button
  55. android:id="@+id/point"
  56. android:text="." />
  57. <Button
  58. android:id="@+id/plus"
  59. android:layout_gravity="fill"
  60. android:layout_rowSpan="2"
  61. android:text="+" />
  62. <Button
  63. android:id="@+id/equal"
  64. android:layout_columnSpan="3"
  65. android:layout_gravity="fill"
  66. android:text="=" />
  67. </GridLayout>

视频建议采用超清模式观看, 欢迎点击订阅我的优酷

Android教程-03 常见布局的总结的更多相关文章

  1. android开发中常见布局的注意点

    常见布局的注意点 线性布局: 必须有一个布局方向 水平或者垂直 在垂直布局中 只有左对齐 右对齐 水平居中生效 在水平布局中 只有顶部对齐 底部对齐 垂直居中生效 权重:组件按比例分配屏幕的剩余部分( ...

  2. [Android]Android之四种常见布局

    一个丰富的界面总是要由很多个控件组成的,那我们如何才能让各个控件都有条不紊地 摆放在界面上,而不是乱糟糟的呢?这就需要借助布局来实现了.布局是一种可用于放置很 多控件的容器,它可以按照一定的规律调整内 ...

  3. Android 布局学习之——Layout(布局)具体解释二(常见布局和布局參数)

     [Android布局学习系列]   1.Android 布局学习之--Layout(布局)具体解释一   2.Android 布局学习之--Layout(布局)具体解释二(常见布局和布局參数)   ...

  4. Android 布局学习之——Layout(布局)详解二(常见布局和布局参数)

    [Android布局学习系列]   1.Android 布局学习之——Layout(布局)详解一   2.Android 布局学习之——Layout(布局)详解二(常见布局和布局参数)   3.And ...

  5. css常见布局方式

    CSS常见布局方式 以下总结一下CSS中常见的布局方式.本人才疏学浅,如有错误,请留言指出. 如需转载,请注明出处:CSS常见布局方式 目录: 使用BFC隐藏属性 float + margin abs ...

  6. Android教程2020 - RecyclerView使用入门

    本文介绍RecyclerView的使用入门.这里给出一种比较常见的使用方式. Android教程2020 - 系列总览 本文链接 想必读者朋友对列表的表现形式已经不再陌生.手机上有联系人列表,文件列表 ...

  7. Xamarin Android教程Android基本知识版本介绍与系统介绍

    Xamarin Android教程Android基本知识版本介绍与系统介绍 Xamarin Android教程Android基本知识版本介绍与系统介绍,开发Andriod有时候不像iOS一样轻松,因为 ...

  8. 【转】Android UI 五种布局

    在一个Android应用中,Layout是开发中的一个很重要环节,Layout是组成UI不可缺少的一部分. ## Android UI 核心类 在Android应用构建UI的方法有以下几种: 单纯使用 ...

  9. Android精通:TableLayout布局,GridLayout网格布局,FrameLayout帧布局,AbsoluteLayout绝对布局,RelativeLayout相对布局

    在Android中提供了几个常用布局: LinearLayout线性布局 RelativeLayout相对布局 FrameLayout帧布局 AbsoluteLayout绝对布局 TableLayou ...

随机推荐

  1. Nginx 编译设置模块执行顺序

    Nginx编译时,配置"--add-module=xxx"可以加入模块,当我们需要按照指定顺序来设置过滤模块执行顺序时,先配置的"--add-module=xxx&quo ...

  2. Win7x64易语言调试进程无法退出

    这是个历史问题,几乎所有的Win7x64机器上都会碰到这个问题 解决方法: 启动黑月重新编译器

  3. NIOP模拟17.10.13

    太水,简述一下题意 T1 让你计算一个形如Σai * bi^ki 快速幂即可 #include <iostream> #include <cstdio> #include &l ...

  4. TP5.1 首页路由

    把自带的return 删了

  5. 模拟4题解 T1礼物

    T1 题目描述 夏川的生日就要到了.作为夏川形式上的男朋友,季堂打算给夏川买一些生 日礼物. 商店里一共有种礼物.夏川每得到一种礼物,就会获得相应喜悦值Wi(每种 礼物的喜悦值不能重复获得). 每次, ...

  6. 如何在CentOS 7 / Fedora 31/30/29上安装ELK Stack

    原文地址:https://computingforgeeks.com/how-to-install-elk-stack-on-centos-fedora/ 原作者: Josphat Mutai 译者: ...

  7. RestFul 与 RPC

    原文地址:https://blog.csdn.net/u014590757/article/details/80233901 RPC.REST API深入理解 一:RPC RPC 即远程过程调用(Re ...

  8. 完整版unity安卓发布流程(包括SDK有原生系统依赖关系的工程)

    要3个东西!NDS,SDK,JDK, NDK官网下载:https://developer.android.google.cn/ndk/downloads/index.html(注意系统是不是64位) ...

  9. Appium 常用的API函数

    常用的API函数[转] http://blog.sina.com.cn/s/blog_68f262210102vzf9.html 获取信息类API (1)获取默认系统语言对应的Strings.xml文 ...

  10. day38 13-Spring的Bean的属性的注入:SpEL注入

    Spring2.5提供了名称空间p注入属性的方式,Spring3.几提供了SpEL属性注入的方式. <?xml version="1.0" encoding="UT ...