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

本文复习了Android中常用的布局,包括LinearLayout、RelativeLayout、FrameLayout、TableLayout、AbsoluteLayout和ContraintLayout。详细介绍了各布局的特点和重要属性,如LinearLayout的方向、权重等,还给出了部分布局的使用示例,同时指出AbsoluteLayout已过时,ContraintLayout可解决布局嵌套问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

前几篇文章复习总结了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是一种允许您以灵活的方式定位和调整小部件的布局

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

转载于:https://juejin.im/post/5c8e248ee51d4556f364d695

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值