Android布局资源的四种常用类型详解

0 阅读4分钟

Android 四种常用布局资源详解

一、LinearLayout(线性布局)

1. 布局特点

  • 子视图按**水平(horizontal)垂直(vertical)**方向依次排列
  • 通过 layout_weight 按比例分配剩余空间
  • 是最简单、最基础的布局容器

2. 关键属性

属性作用
orientation设置排列方向:vertical(垂直)/ horizontal(水平)
gravity控制所有子控件在容器中的对齐方式
layout_gravity控制单个控件在父容器中的对齐方式
layout_weight分配剩余空间的权重,值越大占越多空间

3. 代码示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="按钮1"/>
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="按钮2"/>
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="按钮3"/>
</LinearLayout>

4. 运行效果截图

f8c8f560aeb8fe2ba8c33cf7d210bc44.png

5. 适用场景

  • 简单的列表排列
  • 表单输入界面
  • 按钮组排列
  • 需要按比例分配空间的场景

二、RelativeLayout(相对布局)

1. 布局特点

  • 子视图通过相对于父容器相对于其他子视图来定位
  • 可以有效减少布局嵌套层级,提升性能
  • 布局灵活,适合复杂界面

2. 关键属性

属性作用
layout_alignParentTop与父容器顶部对齐
layout_alignParentBottom与父容器底部对齐
layout_alignParentLeft/Start与父容器左边/起始边对齐
layout_alignParentRight/End与父容器右边/结束边对齐
layout_centerInParent在父容器中水平垂直居中
layout_below位于指定控件下方
layout_above位于指定控件上方
layout_toLeftOf/toStartOf位于指定控件左侧/起始侧
layout_toRightOf/toEndOf位于指定控件右侧/结束侧

3. 代码示例

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/AppTheme"
    android:id="@+id/mainRelativelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="1dp"

    >
    <Button
        android:id="@+id/btn_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"/>
    <Button
        android:id="@+id/btn_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="260dp"/>

    <Button
        android:id="@+id/btn_three"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/btn_two"
        android:layout_marginLeft="-5dp"
        android:layout_marginBottom="151dp"
        android:layout_toRightOf="@id/btn_two"
        android:text="按钮3" />
</RelativeLayout>

4. 运行效果截图

cd82e0ffed1f2d0112e2f19417a96666.png

5. 适用场景

  • 复杂界面的布局
  • 需要元素之间相互定位的场景
  • 需要减少布局嵌套层级的场景

三、TableLayout(表格布局)

1. 布局特点

  • 以**行(TableRow)**为单位组织内容
  • 列数由子控件最多的行自动决定
  • 支持单元格合并、列拉伸/收缩/隐藏
  • 结构清晰,类似 HTML 表格

2. 关键属性

属性作用
stretchColumns拉伸指定列,列索引从0开始,多个用逗号分隔
shrinkColumns收缩指定列,内容过长时压缩该列
collapseColumns隐藏指定列
layout_span单元格合并列数

3. 代码示例

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:stretchColumns="2" >
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:text="按钮1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:text="按钮2" />
    </TableRow>
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:text="按钮3" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:text="按钮4" />
    </TableRow>
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:text="按钮5" />
    </TableRow>
</TableLayout>

4. 运行效果截图

d73fd7143c5624f1b4c64a9a8e171c6d.png

5. 适用场景

  • 表单输入界面
  • 数据展示类界面
  • 需要行列对齐的场景

四、NeonLamp(霓虹灯效果布局)

1. 布局特点

  • 使用 FrameLayout 作为容器,实现视图叠加
  • 通过 shape 资源 绘制渐变光晕效果
  • 结合 textShadow 属性实现文字发光感
  • 纯 XML 即可实现静态霓虹视觉效果

2. 关键属性

属性作用
android:shadowColor设置文字阴影颜色
android:shadowRadius设置阴影模糊半径,值越大发光效果越明显
android:shadowDx/dy设置阴影偏移量,为0时阴影居中产生光晕效果
gradientshape 中的渐变标签,可实现光晕背景

4. 代码示例

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/btn_one"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_gravity="center"
        android:background="#ff0000" />
    <Button
        android:id="@+id/btn_two"
        android:layout_width="220dp"
        android:layout_height="220dp"
        android:layout_gravity="center"
        android:background="#00ff00" />
    <Button
        android:id="@+id/btn_three"
        android:layout_width="140dp"
        android:layout_height="140dp"
        android:layout_gravity="center"
        android:background="#0000ff" />
    <Button
        android:id="@+id/btn_four"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="center"
        android:background="#ff1243" />
    <Button
        android:id="@+id/btn_five"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_gravity="center"
        android:background="#324678" />
</FrameLayout>

5. 运行效果截图

d6a64851116bc779c3b8fbb55dfa6d86.png

6. 适用场景

  • 欢迎页、启动页
  • 需要视觉特效的场景
  • 自定义控件演示

总结对比

布局类型核心特点适用场景
LinearLayout线性排列,权重分配列表、表单、简单排列
RelativeLayout相对定位,灵活布局复杂界面,减少嵌套
TableLayout表格行列结构数据输入、类Excel界面
NeonLampFrameLayout叠加+shape渐变视觉效果、欢迎页面