常见的布局

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

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 水平

参考代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button1" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button2" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button3" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button4" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--layout_weight 比重-->
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button1" /> <Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button2" /> <Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button3" /> <Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button4" /> </LinearLayout>
</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 本元素的右边缘和某元素的的右边缘对齐

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

参考代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--red green blue-->
<TextView
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#cc0000"
android:text="red"
android:gravity="center"
/>
<TextView
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#ffff00"
android:text="yellow"
android:gravity="center"
/>
<TextView
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#ff1100"
android:text="orange"
android:gravity="center"
/>
<TextView
android:id="@+id/tv_center"
android:layout_centerInParent="true"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#ff1100"
android:text="center"
android:gravity="center"
/>
<TextView
android:layout_alignTop="@+id/tv_center"
android:layout_toLeftOf="@+id/tv_center"
android:layout_width="100dp"
android:layout_marginRight="10dp"
android:layout_height="100dp"
android:background="#88ff00"
android:text="left"
android:gravity="center"
/>
<TextView
android:layout_alignTop="@+id/tv_center"
android:layout_toRightOf="@+id/tv_center"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#ff8800"
android:layout_marginLeft="10dp"
android:text="right"
android:gravity="center"
/> <TextView
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#771122"
android:text="bottom"
android:gravity="center"
/>
</RelativeLayout>

FrameLayout

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

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

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

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!--5.0之前 后添加的控件在之前添加控件的上面 5.0之后 如果有按钮 按钮默认都是在最前面-->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="button"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_gravity="left|top" /> </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

参考代码:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="4"
android:orientation="horizontal"
android:rowCount="5">
  
<Button
android:id="@+id/one"
android:text="1" />
  
<Button
android:id="@+id/two"
android:text="2" />
   
<Button
android:id="@+id/three"
android:text="3" />
<Button
android:id="@+id/devide"
android:text="/" />
<Button
android:id="@+id/four"
android:text="4" />
<Button
android:id="@+id/five"
android:text="5" />
   <Button
android:id="@+id/six"
android:text="6" />
<Button
android:id="@+id/multiply"
android:text="×" />
<Button
android:id="@+id/seven"
android:text="7" /> <Button
android:id="@+id/eight"
android:text="8" /> <Button
android:id="@+id/nine"
android:text="9" /> <Button
android:id="@+id/minus"
android:text="-" /> <Button
android:id="@+id/zero"
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:text="0" />
  
<Button
android:id="@+id/point"
android:text="." /> <Button
android:id="@+id/plus"
android:layout_gravity="fill"
android:layout_rowSpan="2"
android:text="+" /> <Button
android:id="@+id/equal"
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:text="=" />
</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. 想真正了解JAVA设计模式看着一篇就够了。 详解+代码实例

    Java 设计模式   设计模式是对大家实际工作中写的各种代码进行高层次抽象的总结 设计模式分为 23 种经典的模式,根据用途我们又可以分为三大类.分别是创建型模式.结构型模式和行为型模式 列举几种设 ...

  2. SpringMVC返回json的问题

    在使用springmvc的时候,如果返回值是String, 返回一个json的字符串,在js里面接收会有问题,不能直接当成json使用,要通过eval来转成json. 就像你在js里面直接定义 var ...

  3. 提交方式get和post有什么区别

    提交方式post和get有什么区别? (1)post是向服务器传送数据:get是从服务器上获取数据. (2)在客户端,get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个 ...

  4. Spring Boot:Boot2.0版本整合Neo4j

    前面介绍了Boot 1.5版本集成Neo4j,Boot 2.0以上版本Neo4j变化较大. 场景还是电影人员关系 Boot 2.0主要变化 GraphRepository在Boot2.0下不支持了,调 ...

  5. 什么? 1XIN = 21BTC

    什么? 1XIN = 21BTC 最初看到这个标题,我还回去考证一下. 原来是 Mixin Network 的宣传广告. BTC 是多少? 2100万枚. XIN 是 100 万枚. 所以才有了 1X ...

  6. python正则表达式应用 去掉标点符号

  7. Leetcode888.Fair Candy Swap公平的糖果交换

    爱丽丝和鲍勃有不同大小的糖果棒:A[i] 是爱丽丝拥有的第 i 块糖的大小,B[j] 是鲍勃拥有的第 j 块糖的大小. 因为他们是朋友,所以他们想交换一个糖果棒,这样交换后,他们都有相同的糖果总量.( ...

  8. 外贸电子商务网站之Prestashop 设置网站基本信息

    顶部banner图,首页各尺寸banner图.社交分享.物流支持.支付方式提醒等等设置 示例版本:1.6.1.11

  9. layer弹出图片的问题

    转载:https://blog.csdn.net/qq_41815146/article/details/81141088 layer下载地址:http://layer.layui.com/ jQue ...

  10. 【JZOJ4744】【NOIP2016提高A组模拟9.2】同余

    题目描述 输入 输出 样例输入 5 2 1 5 2 3 7 1 3 2 1 2 5 3 0 样例输出 2 1 数据范围 解法 题目允许离线,且没有修改操作. 考虑把一个询问拆分成两个形如"a ...