前言
前几篇文章复习总结了Android的四大组件,现在开始复习Android中常用的布局包括以下几种,先从LinearLayout开始说起
- LinearLayout
- RelativeLayout
- FrameLayout
- TableLayout
- AbsoluteLayout
- ContraintLayout
一、LinearLayout(线性布局)
LinearLayout是一种将其子View水平单列或者垂直单行进行排列的布局
线性布局有以下三个重要的属性
android:orientation
取值为vertical或者horizontal,分别表示垂直布局和水平布局android:layout_weight
权重,应用场景主要有需要按比例分配空间或者填充剩余空间android:layout_gravity
重心,当orientation为vertical时left和right生效,为horizontal时top和bottom生效android:divider
分割线图片,需要配合下面那个属性android:showDividers
显示分割线的位置四选一,none、begin、end、middle
假设需要实现把一个EditText和Bottom放置于一行并且EditText填充除了Button以外的所有区域,我们可以使用以下代码实现
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send"/>
</LinearLayout>
复制代码
二、RelativeLayout(相对布局)
RelativeLayout是一种子View能够描述与其它子View或者父View之间相对关系的布局
相对布局有以下几个重要的属性
android:layout_alignTop
与指定View的顶部对齐android:layout_alignBottom
与指定View的底部对齐android:layout_alignLeft
与指定View的左边对齐android:layout_alignRight
与指定View的右边对齐android:layout_alignParentTop
与父View的顶部对齐android:layout_alignParentBottom
与父View的底部对齐android:layout_alignParentLeft
与父View的左边对齐android:layout_alignParentRight
与父View的顶部对齐android:layout_toLeftOf
当前View的Right与指定View的左边对齐android:layout_toRightOf
当前View的Left与指定View的右边对齐android:layout_above
当前View的Bottom与指定View的上面对齐android:layout_alignBaseLine
当前View的文本底部与指定View的文本底部对齐android:layout_centerHorizontal
是否水平居中android:layout_centerVertical
是否垂直居中android:layout_centerInParent
是否水平垂直居中于父View
这里以列表中的一个Item为例左边是一张图片,右边上面是一个Title,下面是subTitle
<RelativeLayout 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"
tools:context=".MainActivity">
<ImageView
android:id="@+id/iv"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:background="@mipmap/ic_launcher"
android:layout_alignParentLeft="true"/>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/iv"
android:layout_alignTop="@id/iv"
android:text="我是标题"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above=""
android:layout_toRightOf="@id/iv"
android:layout_below="@+id/tv_title"
android:text="我是子标题"/>
</RelativeLayout>
复制代码
三、FrameLayout(帧布局)
FrameLayout是一种将每个子View当做一帧,层叠显示的View
该布局没有比较特别的属性,就只是一层层的叠加
四、TableLayout(表格布局)
TableLayout继承与LinearLayout,将其子View横向或者竖向排列,一般子View是TableRow
表格布局有以下几个重要的属性
android:collapseColumns
隐藏那几列,比如0,2表示隐藏第1、3两列android:stretchColumns
设置哪些列可以被拉伸,如果水平方向还有空余的空间则拉伸这些列android:shrinkColumns
设置哪些列可以收缩,当水平方向空间不够时会进行收缩
五、AbsoluteLayout(绝对布局)
AbsoluteLayout由于无法适配屏幕在API3已经被标为过时了
六、ContraintLayout(约束布局)
ContraintLayout是一种允许您以灵活的方式定位和调整小部件的布局
使用该布局能够显著的解决布局嵌套过多的问题。
- 通过可视化布局参考这篇文章blog.youkuaiyun.com/guolin_blog…
- 通过代码布局参考这篇文章www.jianshu.com/p/17ec9bd6c…