目录

布局管理器

RelativeLayout

常见属性

Relative的实践操作(实现软件更新界面)

LinearLayout

常见属性

LinearLayout的实践操作(模范登录以及微信底部)

FrameLayout

常用属性

FrameLayout实例

TableLayout

常用属性(继承LinearLayout)

TableLayout实例(计算器·的实例)

GridLayout

常用属性

GridLayout实例

网格布局管理器和表格布局管理器的区别:


布局管理器

Android提供五种布局管理器:

1.相对布局管理器(RelativeLayout):通过相对定位的方式来控制组件的摆放位置。

2.线性布局管理器(LinearLayout):是指在水平或者垂直方向上依次摆放组件。

3.帧布局管理器(FrameLayout):没有任何定位方式,默认情况下,所有的组件都会摆放在容器的左上角,逐个覆盖。

4.表格布局管理器(TableLayout):使用表格的方式按行、列来摆放组件。

5.绝对布局管理器(AbsoluteLayout):通过绝对定位(x,y坐标)的方式来控制组件的摆放位置。(过期)

6.网格布局管理器(GridLayout):通过它可以实现跨行和跨列摆放组件。

Android提供的布局管理器均直接或间接地继承ViewGroup类。

说明:

在Android中,无论是创建哪一种布局都有两种方法,一种是在XML布局文件当中定义,另一种是使用Java代码来创建。推荐使用XML布局文件中定义。

RelativeLayout

常见属性

  1. android:layout_above="@id/xxx" --将控件置于给定ID控件之上

  2. android:layout_below="@id/xxx" --将控件置于给定ID控件之下

  3. android:layout_toLeftOf="@id/xxx" --将控件的右边缘和给定ID控件的左边缘对齐

  4. android:layout_toRightOf="@id/xxx" --将控件的左边缘和给定ID控件的右边缘对齐

  5. android:layout_alignLeft="@id/xxx" --将控件的左边缘和给定ID控件的左边缘对齐

  6. android:layout_alignTop="@id/xxx" --将控件的上边缘和给定ID控件的上边缘对齐

  7. android:layout_alignRight="@id/xxx" --将控件的右边缘和给定ID控件的右边缘对齐

  8. android:layout_alignBottom="@id/xxx" --将控件的底边缘和给定ID控件的底边缘对齐

  9. android:layout_alignParentLeft="true" --将控件的左边缘和父控件的左边缘对齐

  10. android:layout_alignParentTop="true" --将控件的上边缘和父控件的上边缘对齐

  11. android:layout_alignParentRight="true" --将控件的右边缘和父控件的右边缘对齐

  12. android:layout_alignParentBottom="true" --将控件的底边缘和父控件的底边缘对齐

  13. android:layout_centerInParent="true" --将控件置于父控件的中心位置

  14. android:layout_centerHorizontal="true" --将控件置于水平方向的中心位置

  15. android:layout_centerVertical="true" --将控件置于垂直方向的中心位置

Relative的实践操作(实现软件更新界面)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/design_default_color_secondary_variant"tools:context=".MainActivity"android:padding="16dp">
​
​<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="发现有Widget的新版本,您现在就安装吗?"android:id="@+id/text1"android:layout_centerInParent="true">
​</TextView><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/button1"android:text="以后再说"android:layout_alignRight="@+id/text1"android:layout_below="@+id/text1">
​</Button><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="现在更新"android:layout_toLeftOf="@+id/button1"android:layout_below="@+id/text1"></Button>
​
</RelativeLayout>

LinearLayout

常见属性

1.layout_gravity 设置布局中控件的位置 top 上 bottom 下 left 左 right 右 center_vertical 垂直居中 center_horizontal 水平居中 center 居中

2.weight(权重)属性

用来设置占布局所占布局得的比重 (1)layout_width都为0时 按照所设置的比重来分配权重 比如:三个控件 1 2 3 weight分比为1、2、3 layout_width = "0dp" 则会将屏幕的大小分为1+2+3=6份 三个控件分别占布局的1/6 2/6 3/6

(2)layout_width都为warp_content时 这种情况下和上边的情况一样

(3)layout_width为match_parent时是最复杂的一种情况 因为此时控件所占的所有宽度大于容器的宽度 此时要计算每个空间所占的比例 计算公式: 额外的空间=手机的宽度(高度)-所有控件的宽度(高度) 控件的宽度(高度)=控件的width(height)值+(该控件的weight值/所有控件的weight的和)×额外的空间

以上边的情况为例子: 设 match_parent为x 额外空间 = x - 3x = -2x 控件1宽度 = x+ 1/6*(-2x) = 4/6x =1/3x 所以控件1占屏幕的1/3

3.添加分割线

(1)通过view添加 <View android:layout_width="match_parent" android:layout_height="1px" android:background="#000000" />

(2)通过背景图片添加 android:divider="@drawable/ktv_line_div" android:showDividers="middle" android:dividerPadding="10dp" 1)android:divider设置作为分割线的图片 2)android:showDividers设置分割线的位置,none(无), begining(开始),end(结束),middle(每两个组件间) 3)dividerPadding设置分割线的Padding

LinearLayout的实践操作(模范登录以及微信底部)

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:id="@+id/line"
​tools:context=".LinearLayout">
​<EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:paddingBottom="20dp"android:hint="QQ号/微信号/Email"></EditText>
​<EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:paddingBottom="20dp"android:hint="密码"></EditText>
​<Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="登录"></Button>
​<TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="登录遇到问题?"android:gravity="center"></TextView>
​<LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"></LinearLayout>
​<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="bottom"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="首页"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="联系人"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="朋友圈"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="我"></Button></LinearLayout>
</LinearLayout>

FrameLayout

常用属性

1.android:foreground :设置该帧布局管理器的前景图像。

2.android:foregroundGravity:定义绘制前景图像的gravity属性,即前景图像显示的位置。

FrameLayout实例

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_width="match_parent"android:layout_height="match_parent"tools:context=".FrameLayout">
​<TextViewandroid:layout_width="280dp"android:layout_height="280dp"android:background="#ff0000ff"android:layout_gravity="center"android:textColor="#ffffff"android:text="蓝色背景板的TextView"></TextView>
​<TextViewandroid:layout_width="230dp"android:layout_height="230dp"android:background="#ff0077ff"android:layout_gravity="center"android:textColor="#ffffff"android:text="天蓝色背景板的TextView"></TextView>
​<TextViewandroid:layout_width="180dp"android:layout_height="180dp"android:background="#ff00b4ff"android:layout_gravity="center"android:text="水蓝色背景板的TextView"android:textColor="#ffffff"></TextView>
</FrameLayout>

TableLayout

常用属性(继承LinearLayout)

1.android:collapseColumns:将TableLayout里面指定的列隐藏,若有多列需要隐藏,请用逗号将需要隐藏的列序号隔开。

2.android:stretchColumns:设置指定的列为可伸展的列,以填满剩下的多余空白空间,若有多列需要设置为可伸展,请用逗号将需要伸展的列序号隔开。

3.android:shrinkColumns:设置指定的列为可收缩的列。当可收缩的列太宽(内容过多)不会被挤出屏幕。当需要设置多列为可收缩时,将列序号用逗号隔开。

TableLayout实例(计算器·的实例)

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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:layout_width="match_parent"android:layout_height="match_parent"tools:context=".TableLayout"><EditTextandroid:layout_height="80dp"android:hint="0"android:layout_width="match_parent"></EditText><TableRowandroid:layout_width="wrap_content"android:layout_height="60dp"android:padding="10dp"><Buttonandroid:layout_height="match_parent"android:layout_width="wrap_content"android:layout_weight="1"android:text="AC"></Button>
​<Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="DEL"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="%"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="*"></Button></TableRow><TableRowandroid:layout_width="wrap_content"android:layout_height="60dp"android:padding="10dp"><Buttonandroid:layout_height="match_parent"android:layout_width="wrap_content"android:layout_weight="1"android:text="7"></Button>
​<Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="8"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="9"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="/"></Button></TableRow><TableRowandroid:layout_width="wrap_content"android:layout_height="60dp"android:padding="10dp"><Buttonandroid:layout_height="match_parent"android:layout_width="wrap_content"android:layout_weight="1"android:text="4"></Button>
​<Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="5"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="6"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="+"></Button></TableRow><TableRowandroid:layout_width="wrap_content"android:layout_height="60dp"android:padding="10dp"><Buttonandroid:layout_height="match_parent"android:layout_width="wrap_content"android:layout_weight="1"android:text="1"></Button>
​<Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="2"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="3"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="-"></Button></TableRow><TableRowandroid:layout_width="wrap_content"android:layout_height="60dp"android:padding="10dp"><Buttonandroid:layout_height="match_parent"android:layout_width="wrap_content"android:layout_weight="1"android:text="0"></Button>
​<Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="."></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_span="2"android:layout_weight="1"android:text="="></Button></TableRow>
</TableLayout>

GridLayout

常用属性

1.android:columnCount: 最大列数

2.android:rowCount:最大行数

3.android:orientation: GridLayout中子元素的布局方向

4.android:alignmentMode alignBounds:对齐子视图边界 alignMargins :对齐子视距内容,默认值

5.android:columnOrderPreserved: 使列边界显示的顺序和列索引的顺序相同,默认是true

6.android:rowOrderPreserved: 使行边界显示的顺序和行索引的顺序相同,默认是true

7.android:useDefaultMargins: 没有指定视图的布局参数时使用默认的边距,默认值是false

GridLayout实例

<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"android:columnCount="4"android:rowCount="5"tools:context=".GridLayout">
​<Buttonandroid:text="1"></Button><Buttonandroid:text="2"></Button><Buttonandroid:text="3"></Button><Buttonandroid:text="/"></Button><Buttonandroid:text="4"></Button><Buttonandroid:text="5"></Button><Buttonandroid:text="6"></Button><Buttonandroid:text="*"></Button><Buttonandroid:text="7"></Button><Buttonandroid:text="8"></Button><Buttonandroid:text="9"></Button><Buttonandroid:text="-"></Button><Buttonandroid:layout_columnSpan="2"android:layout_gravity="fill"android:text="0"></Button><Buttonandroid:text="."></Button><Buttonandroid:layout_rowSpan="2"android:layout_gravity="fill_vertical"android:text="+"></Button><Buttonandroid:text="="android:layout_columnSpan="3"android:layout_gravity="fill"></Button>
</GridLayout>

网格布局管理器和表格布局管理器的区别:

1.网格布局管理器可以跨行或者跨列,但是表格布局管理器只能和跨行。

2.网格布局管理器可以实现一行占满后超出容器的组件将自动换行,而表格布局管理器超出容器的组件将不会被显示。

Android中常见五种布局管理器——RelativeLayout、LinearLayout、FrameLayout、TableLayout、GridLayout相关推荐

  1. Android中的6大布局管理器

    文章目录 6大布局管理器详解 1. LinearLayout 线性布局管理器 2. TableLayout 表格布局管理器 3. GridLayout 网格布局管理器 ~~~~~~~~~~~~~~~~ ...

  2. java的布局管理器_Java中提供了几种布局管理器

    近日,很多网友都在关注Java中提供了几种布局管理器这个话题,那么Java中提供了几种布局管理器具体情况是怎么样的呢?Java中提供了几种布局管理器的相关信息有哪些?下面的内容是小编为大家找到的关于J ...

  3. Android UI详解之布局管理器(一)

    Android UI详解之布局管理器 一.布局管理器 ①顶级父类View ②子类GroupView ③AbsoluteLayout.FrameLayout.LinearLayout.GridLayou ...

  4. 相对布局管理器RelativeLayout

    介绍 相对布局管理器是通过相对定位的方式让组件出现在布局的任何位置的.例如如图 3.14 所示的界面就是采用相对布局管理器来进行布局的,其中先放置组件 A,然后放置组件 B,让其位于组件 A 的下方, ...

  5. JAVA共有几种窗体布局方式_在Java GUI程序开发中常见的三种布局管理器是什么

    答:FlowLayout 流式布局,从左到右,如果到边界就换行再从左到右. BorderLayout 边界布局(默认布局方式),按东西南北中五个方向来布局,默认是中.后设置在同样位置的控件会覆盖之前的 ...

  6. Java AWT中常用的三种布局管理器

    文章目录 布局管理器 一.流程布局管理器(FlowLayout) 二.边界布局管理器(BorderLayout) 三 .网格布局管理器 四. 综合实例运用 布局管理器 在java.awt 包中提供了5 ...

  7. Android布局管理器-使用LinearLayout实现简单的登录窗口布局

    场景 Android布局管理器-从实例入手学习相对布局管理器的使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1038389 ...

  8. Android常用的五种布局

    开发工具与关键技术:Android studio64, Android 1.FrameLayout(帧布局)是最简单的布局方式,放置的控件都只能罗列到左上角,控件会有重叠,不能进行复杂的布局. 2.L ...

  9. JAVA布局管理器导包_在 Java 中,要使用布局管理器,必须导入下列( )包。_计算机网络基础答案_学小易找答案...

    [单选题]详图索引符号中的分子编号4代表( ). [单选题]MCS - 51 单片机的 CPU 主要的组成部分为( ) [判断题]路由器工作在物理层,其实现路径选择和寻址的功能.( ); [单选题]禅 ...

最新文章

  1. logback的使用和logback.xml详解
  2. Windows下OpenSSL创建CA证书以及客户端和服务器端证书
  3. springboot tomcat默认线程数_记一次JAVA线程池的错误用法
  4. html基本结构(头部需加上样式表),HTML基本结构、头部、注释(示例代码)
  5. 技巧 | 如何使用R语言的基础绘图系统的拼图功能
  6. 分享一个真正高收益,一本万利的行业
  7. Python with 结构
  8. 通过aspnetpager为DataList分页
  9. matlab学习笔记1
  10. ios手机怎么连接adb命令_Mac连接Adb
  11. uniapp编译支付宝小程序图片图标显示问题
  12. cass有坐标文件生成里程文件_南方CASS里程文件生成
  13. Win10控制网卡启停脚本
  14. Attiny48单片机编程经验总结
  15. CDM以及ACDM相关的时间、时刻专有词汇说明
  16. [译]基于Vue JS, Webpack 以及Material Design的渐进式web应用 [Part 1]
  17. Tumblr营销大法(一)
  18. 图表生成pdf,出坑经历
  19. php的set 方法调用,PHP中 对象自动调用的方法:set()、get()、tostring()
  20. python解析gff文件中的转录本

热门文章

  1. Linux的tar命令
  2. 全面剖析雅虎助手以及网络实名的流氓行径(6)
  3. CrackMe 之 006
  4. SQLServer 常用函数汇总
  5. 文章原创度检测 api数据接口
  6. web端meta标签的各种状态的设定
  7. Microsoft Office SharePoint Server 2007的文件目录结构
  8. Android9.0 Fiddler 模拟器抓包
  9. 离散方法(一)——有限单元方法(FEM)
  10. 如何把视频kux格式转换mp4 手机怎么观看kux视频