Android中常见的布局和布局参数

前言

前几篇文章复习总结了Android的四大组件,如今开始复习Android中经常使用的布局包括如下几种,先从LinearLayout开始提及android

  • LinearLayout
  • RelativeLayout
  • FrameLayout
  • TableLayout
  • AbsoluteLayout
  • ContraintLayout

1、LinearLayout(线性布局)

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>
复制代码

2、RelativeLayout(相对布局)

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>
复制代码

3、FrameLayout(帧布局)

FrameLayout是一种将每一个子View当作一帧,层叠显示的Viewblog

该布局没有比较特别的属性,就只是一层层的叠加继承

4、TableLayout(表格布局)

TableLayout继承与LinearLayout,将其子View横向或者竖向排列,通常子View是TableRow图片

表格布局有如下几个重要的属性

  • android:collapseColumns 隐藏那几列,好比0,2表示隐藏第一、3两列
  • android:stretchColumns 设置哪些列能够被拉伸,若是水平方向还有空余的空间则拉伸这些列
  • android:shrinkColumns 设置哪些列能够收缩,当水平方向空间不够时会进行收缩

5、AbsoluteLayout(绝对布局)

AbsoluteLayout因为没法适配屏幕在API3已经被标为过期了

6、ContraintLayout(约束布局)

ContraintLayout是一种容许您以灵活的方式定位和调整小部件的布局

使用该布局可以显著的解决布局嵌套过多的问题。

相关文章
相关标签/搜索