2.1View概述
GUI界面由View和VIewGroup对象构建。多个View(视图组件)可以放在ViewGruop(视图容器)中。View是Android所有与用户交互控件的父类(文本框、按钮、ViewGroup类布局控件等)因此它包含的XML属性和方法是所有组件都可以使用的。
注意:一个界面文件有且仅有一个容器作为根节点。

2.2页面布局
2.2.1创建使用布局文件
1.点击layout文件夹->New->XML->Layout XML File。
2.使用方式可以通过图形界面拖拉控件,或进行源码编写。
2.2.2页面布局
注意:px、pt分别为屏幕中可以显示的最小元素单元和字体单位面积。无法保证适配性。在设置控件和字体大小时推荐使用dp(密度无关像素)和sp。使用dp的好处是在无论屏幕的分辨率如何总能显示相同的大小。
<titleName xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
/>
//xmlns为命名空间,命名空间android用于 Android 系统定义的一些属性。(缺少会报错无法正常进行布局)
//tools为IDEA相关信息,app为Android自定义属性和系统扩展属性
2.2.3相对布局(RelativeLayout)


2.2.4线性布局(LinearLayout)
<LinearLayout android:orientation = "horizontal"/></LinearLayout>//线性水平
<LinearLayout android:orientation = "vertical"></LinearLayout>//线性竖直
2.2.5表格布局(TableLayout)
使用android:stretchColumns=“1”,属性拉伸第二列,android:layout_column=“0”属性表示显示在第一列中。 需要注意的是,TableRow不需要设置宽度和高度,其宽度一定是自动填充容器,高度根据内容改变。但对于TableRow的其他控件来说,是可以设置宽度和高度的。
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow>
<Button/>
</TableRow>
<TableRow>
<Button/>
<Button/>
<Button/>
</TableRow>
</TableLayout>
//表格布局行数由TableRow决定,列数由最大组件数决定。实例为2*3表格
//在控件中使用layout_column属性指定具体的列数,该属性的值从0开始,代表第一列。
2.2.6网格布局(GridLayout)
类似TableLayout,无TableRow。
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="4" //设置行数
android:orientation="vertical" //设置排序方向
android:rowCount="4"> //设置列数
<Button
android:layout_row="0"
android:layout_column="0" /> //设置控件位置
<Button
android:layout_rowSpan="2"
android:layout_columnSpan="2" /> //设置占位大小
</GridLayout>
2.2.7绝对布局(AbsoluteLayout)
绝对布局是通过指定x、y坐标来控制每一个控件的位置,放入该布局的控件需要通过android:layout_x和android:layout_y两个属性指定其在屏幕上确切的位置。把屏幕看作一个坐标轴,左上角为(0,0),往屏幕下方为y正半轴,右方为x正半轴
2.2.8帧布局(FrameLayout)
帧布局是Android布局中最简单的一种,帧布局为每个加入其中的控件创建了一块空白区域。采用帧布局的方式设计界面时,只能在屏幕左上角显示一个控件,如果添加多个控件,这些会依次重叠在屏幕左上角显示,且会透明显示之前的文本。
2.2.8扁平化布局(ConstraintLayout)
ConstraintLayout非常适合使用可视化的方式来编写界面,但并不太适合使用XML的方式来进行编写。
2.3Android常用控件

