【Android学习札记】Android七大布局及主要属性

来源:互联网 发布:cfree软件下载 编辑:程序博客网 时间:2024/06/08 07:12

一、UI组件类图

这里写图片描述

这是在网上找的,图比较老,有很多新类没在上面;可以看到:所有控件都直接或间接继承自View类,而所有布局都直接或间接继承自ViewGroup类;

二、七大布局

1,线性布局(LinearLayout)

1) 它包含的子控件将以横向或者竖向的方式排列,线性布局内的控件不换行or换列,组件依次排列,超出容器的控件则不会被显示

2) 重要属性:
这里写图片描述

2,相对布局(RelativeLayout)

1)相对布局容器内子组件的位置总是相对兄弟组件、父容器来决定的。

2)重要属性:
这里写图片描述

3,绝对布局(AbsoluteLayout)

1)该布局又可以叫做坐标布局,可以直接指定子元素的绝对位置(x,y)
但是由于手机屏幕尺寸差别较大,使用绝对定位的适应性会比较差,在屏幕的适配上有缺陷

2) 重要属性:
layout_x:指定该子组件的X坐标。
layout_y:指定该子组件的Y坐标。

注:在开发Android应用时不建议使用决定布局,会难兼顾不同屏幕大小、分辨率的问题。

4,表格布局(TableLayout)

1)表格布局采用行、列的形式来管理UI组件,TableLayout并不需要明确地声明包含多少行、多少列,而是通过添加TableRow、其他组件来控制表格的行数和列数。(TableRow就是一个表格行,也是容器)。
表格的宽度由该列中最宽的那个单元格决定,整个表格布局的宽度则取决于福容器的宽度。

2)单元格设置的三种行为方式:
1.Shrinkable:如果某个列被设定为Shrinkable,那么该列的所有单元格的宽度可以被收缩,以保证该表格能适应父容器的宽度。(android:shrinkColumns)
2.Stretchable:如果某个列被设为Stretchable,那么该列的所有单元格的宽度可以被拉伸,以保证组件能完全填满表格空余空间。(android:stretchColumns)
3.Collapsed:如果某个列被设为Stretchable,那么该列的所有单元格会被隐藏。(android:collapseColumns)

3)重要属性:
这里写图片描述

5,帧布局(FrameLayout)

1)它为每个加入其中的组件创建一个空白的区域(称为一帧),所有每个子组件占据一帧,这些帧都会根据gravity属性执行自动对齐,如果对齐方式一样,后面的子元素直接覆盖在前面的子元素上,将前面的子元素部分和全部遮挡。
2)主要属性:
android:foreground 设置该帧布局容器的前景图像。
android:foregroundGravity 定义绘制前景图像的gravity属性。

6,网格布局(GridLayout)

1)该布局是Android 4.0 后新增的一个布局,以网格的形式布局窗口控件

网格布局和其他布局不同,可以不为组件设置Layout_width和Layout_height属性
因为组件的宽高由几行几列决定了,当然,你也可以写个wrap_content

2)主要属性:
A:排列对齐:
①设置组件的排列方式: android:orientation=”” vertical(竖直,默认)或者horizontal(水平)
②设置组件的对齐方式: android:layout_gravity=”” center,left,right,buttom啊这些,如果想同时用两种的话:eg: buttom|left

B:设置布局为几行几列:
①设置有多少行:android:rowCount=”4” //设置网格布局有4行
②设置有多少列:android:columnCount=”4” //设置网格布局有4列

C:设置某个组件位于几行几列
①组件在第几行:android:layout_row = “1” //设置组件位于第二行
②组件在第几列:android:layout_column = “2” //设置该组件位于第三列

注意:从0开始算

D:设置某个组件横跨几行几列:
①横跨几行:android:layout_rowSpan = “2” //纵向横跨2行
②横跨几列:android:layout_columnSpan = “3” //横向横跨2列

7,百分比布局

1)可以不用再使用wrap_content、match_parent等方式来指定控件的大写,而允许直接指定控制在布局中所占的百分比;

2)提供了两张全新的布局:PercentRelativeLayout、PercentFrameLayout(LinearLayout本身已经有weight属性)

3)支持的属性有:
layout_widthPercent、layout_heightPercent、
layout_marginPercent、layout_marginLeftPercent、
layout_marginTopPercent、layout_marginRightPercent、
layout_marginBottomPercent、layout_marginStartPercent、layout_marginEndPercent。

4)为了支持所有版本,android团队将百分百布局定义在suport库中,所以在使用前记得在build.gradle添加百分百的依赖:

 compile 'com.android.support:percent:22.2.0'

5)PercentFrameLayout代码示例:

<?xml version="1.0" encoding="utf-8"?><!--由于百分百布局并不是内置在系统SDK中,所以需要完整的包路径;还必须定义一个app的命名空间--><android.support.percent.PercentFrameLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="0dp"        android:layout_height="0dp"        android:layout_gravity="left|top"        android:background="#44ff0000"        android:text="width:30%,height:20%"        app:layout_heightPercent="20%"        android:gravity="center"        app:layout_widthPercent="30%"/>    <TextView        android:layout_width="0dp"        android:layout_height="0dp"        android:layout_gravity="right|top"        android:gravity="center"        android:background="#4400ff00"        android:text="width:70%,height:20%"        app:layout_heightPercent="20%"        app:layout_widthPercent="70%"/>    <TextView        android:layout_width="0dp"        android:layout_height="0dp"        android:layout_gravity="bottom"        android:background="#770000ff"        android:text="width:100%,height:10%"        android:gravity="center"        app:layout_heightPercent="10%"        app:layout_widthPercent="100%"/></android.support.percent.PercentFrameLayout>
0 0
原创粉丝点击