前几篇文章复习总结了Android的四大组件,如今开始复习Android中经常使用的布局包括如下几种,先从LinearLayout开始提及android
LinearLayout是一种将其子View水平单列或者垂直单行进行排列的布局ide
线性布局有如下三个重要的属性布局
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之外的全部区域,咱们可使用如下代码实现spa
<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是一种子View可以描述与其它子View或者父View之间相对关系的布局.net
相对布局有如下几个重要的属性code
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,下面是subTitlexml
<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是一种将每一个子View当作一帧,层叠显示的Viewblog
该布局没有比较特别的属性,就只是一层层的叠加继承
TableLayout继承与LinearLayout,将其子View横向或者竖向排列,通常子View是TableRow图片
表格布局有如下几个重要的属性
android:collapseColumns
隐藏那几列,好比0,2表示隐藏第一、3两列android:stretchColumns
设置哪些列能够被拉伸,若是水平方向还有空余的空间则拉伸这些列android:shrinkColumns
设置哪些列能够收缩,当水平方向空间不够时会进行收缩AbsoluteLayout因为没法适配屏幕在API3已经被标为过期了
ContraintLayout是一种容许您以灵活的方式定位和调整小部件的布局
使用该布局可以显著的解决布局嵌套过多的问题。