Android中LinearLayout线性布局使用详解
LinearLayout(线性布局)是Android中最基础、最常用的布局之一,它按照水平或垂直方向依次排列子视图。
基本特性
- 方向性:可以设置为水平(
horizontal
)或垂直(vertical
)排列 - 权重:支持通过
weight
属性分配剩余空间 - 简单高效:布局计算简单,性能较好
- 嵌套组合:常与其他布局嵌套使用实现复杂界面
基本属性
核心属性
-
android:orientation
:布局方向vertical
:垂直排列(默认)horizontal
:水平排列
-
android:gravity
:子视图在布局内的对齐方式(定义在容器视图上)top
子视图顶部对齐/bottom
子视图底部对齐/left
子视图左对齐/right
子视图右对齐center_vertical
垂直居中/center_horizontal
水平居中/center
水平居中- 可以组合使用,如
left|center_vertical
-
android:layout_gravity
:单个子视图在布局内的对齐方式(取值同上,定义在子视图上)
权重属性
android:layout_weight
:子视图的权重,用于分配剩余空间- 值越大,分配的空间越多(
android:layout_weight:1
类似于css中的flex:1
) - 常与
layout_width
或layout_height
设为0dp
配合使用
- 值越大,分配的空间越多(
基本用法示例
垂直布局示例
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:padding="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3"/>
</LinearLayout>
水平布局示例
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是一个水平排列的文本"
android:layout_marginStart="8dp"/>
</LinearLayout>
权重(weight)的高级用法
权重是LinearLayout最强大的特性之一,可以实现比例分配空间。
等分空间
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
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="1"
android:text="按钮3"/>
</LinearLayout>
不等比例分配
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:text="占2/5空间"
android:background="#FF5722"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:text="占3/5空间"
android:background="#4CAF50"/>
</LinearLayout>
常用技巧
1. 分割线使用
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:showDividers="middle|beginning|end"
android:divider="@drawable/divider_line"
android:dividerPadding="8dp">
<!-- 子视图 -->
</LinearLayout>
需要定义divider_line的drawable:
<!-- res/drawable/divider_line.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="1dp" android:height="1dp"/>
<solid android:color="#CCCCCC"/>
</shape>
2. 基线对齐(针对文本)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="true">
<!-- 不同大小的文本会按照基线对齐 -->
</LinearLayout>
3. 嵌套使用实现复杂布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 顶部标题栏 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal"
android:background="#3F51B5">
<!-- 标题栏内容 -->
</LinearLayout>
<!-- 中间内容区 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<!-- 左侧导航 -->
<LinearLayout
android:layout_width="120dp"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 导航内容 -->
</LinearLayout>
<!-- 右侧内容 -->
<ScrollView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<!-- 可滚动内容 -->
</ScrollView>
</LinearLayout>
<!-- 底部按钮栏 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal">
<!-- 底部按钮 -->
</LinearLayout>
</LinearLayout>
代码中动态修改LinearLayout
LinearLayout linearLayout = findViewById(R.id.my_linear_layout);
// 修改方向
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
// 添加子视图
Button newButton = new Button(this);
newButton.setText("动态添加的按钮");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.weight = 1; // 设置权重
linearLayout.addView(newButton, params);
性能优化建议
- 减少嵌套:避免多层LinearLayout嵌套,会增加布局计算复杂度
- 合理使用weight:weight会触发两次测量,影响性能
- 考虑使用ConstraintLayout:对于复杂布局,ConstraintLayout通常性能更好
- 使用merge标签:当LinearLayout是根布局时,可以使用减少视图层级
常见问题解决
Q1:为什么设置了weight但视图不显示?
A:确保对应的width/height设置为0dp
Q2:如何让最后一个按钮靠右对齐?
A:可以在按钮前添加一个空白View:
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="靠右按钮"/>
Q3:如何实现wrap_content但又限制最大宽度?
A:使用android:maxWidth
属性或结合ConstraintLayout