1:在android中共有五大布局,它们分别是:
- FrameLayout (帧布局)
- LinearLayout (线性布局)
- AbsoluteLayout (绝对布局)
- RelativeLayout (相对布局)
- GridLayout (网格布局)
1) FrameLayout (帧布局) 特点 : 帧布局有点类似于awt的CardLayout都是把组件一个一个叠加到一起,且无法为这些元素指定一个确切的位置,而且下一个元素还会覆盖上一个元素,适合用于浏览单张图片。
2)LinearLayout(线性布局)特点: 现在用的比较多
2.1: 首先声明只有在Linearlayout中,layout_weight属性才有效。
2.2:它是用来指定(剩余空闲空间)的分割比例,而非按比例分配整个空间。
2.3:另外android:layout_weight会引起争议,是因为在设置该属性的同时,
设置android:layout_width为wrap_content和match_parent会造成两种截然相反的效果。
<?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:background="@color/colorPrimary"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="btn 1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="btn 2"
/>
</LinearLayout>
由此图我们可以看出 两个Button按钮的比列是在水平方向上 为 1 :3
3) AbsoluteLayout(绝对布局)特点:(基本不用)
采用坐标轴的方式定位组件,左上角是(0,0)点,往右x轴递增,往下Y轴递增,组件定位属性为android:layout_x和 android:layout_y来确定坐标。 适合用于准确定位空间的位置
4)RelativeLayout (相对布局)特点:
相对布局窗口内子组件的位置总是相对兄弟组件、父容器来决定的,因此叫相对布局 也就是说以某一个组件为参照物,来定位下一个组件的位置的布局方式。适合控件之间存在相应关系是使用 (推荐使用)
控制居中的:
android:layout_centerInParent (父)水平,垂直同时居中
android:layout_centerHorizontal 水平居中
android:layout_centerVertical 垂直居中
控制贴边的:
android:layout_alignParentLeft 当前控件的左边紧贴父控件的左边
android:layout_alignParentTop 当前控件的顶边紧贴父控件的顶边
android:layout_alignParentRight 当前控件的右边紧贴父控件的右边
android:layout_alignParentBottom 当前控件的底边紧贴父控件的底边
5)GridLayout (网格布局)特点:
5.1. 整体的页面会按要求分为n行n列的网格,可以将控件直接放置在指定的行和列中
5.2. 网格也可以实现跨行或跨列显示,如一个网格的高度可以直接占据两行的高度
5.3. 可以省略layout_width和layout_height属性
android:rowCount="4" 指定总行数
android:columnCount="4" 指定总列数
指定控件位于网格的第几行第几列中
android:layout_row="0"
android:layout_column="0"
android:layout_rowSpan="2" 将控件的高度拉伸为2行的高度
android:layout_columnSpan="2" 将控件的宽度拉伸为2列的宽度
注意: 以上两个拉伸的属性必须配合android:layout_gravity="fill"才能生效
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="4"
android:columnCount="4"
>
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:text="1"
android:background="#ff0000"
android:layout_row="0"
android:layout_column="0"
/>
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:text="1"
android:background="#ff0000"
android:layout_row="0"
android:layout_column="1"
/>
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:text="2"
android:background="#00ff00"
android:layout_row="1"
android:layout_column="0"
android:layout_columnSpan="2"
android:layout_gravity="fill"
/>
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="3"
android:background="#0000ff"
android:layout_row="0"
android:layout_column="2"
android:layout_rowSpan="2"
android:layout_gravity="fill"
/>
</GridLayout>