好的,阿杰 👍
我帮你整理一篇 Android 布局知识点剖析系列:深入了解 layout_weight 属性,从基础概念到实战技巧,全方位讲解。


Android 布局知识点剖析系列 —— layout_weight 深入解析

1. 基本概念

  • layout_weightLinearLayout 的属性,用于分配子 View 在主轴方向(水平或垂直)上的剩余空间
  • 简单理解:
    • 当一个 LinearLayout 的子控件宽度/高度设为 0dp(或 wrap_content 配合权重),通过 layout_weight 来决定 占剩余空间的比例
  • 主轴方向:
    • LinearLayout 水平 → weight 分配 宽度
    • LinearLayout 垂直 → weight 分配 高度

2. 基本使用方法

例 1:水平 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" />
</LinearLayout>

  • 解释
    • layout_width="0dp":宽度由 weight 决定
    • layout_weight="1":两个按钮平均分剩余空间

例 2:不同权重

<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="2"
        android:text="按钮2" />
</LinearLayout>

  • 解释
    • 按钮1 : 按钮2 = 1 : 2
    • 剩余空间按权重比例分配

3. weight 与 wrap_content / match_parent

情况效果
0dp + weight空间完全由 weight 分配,推荐做自适应布局
wrap_content + weight先显示控件内容大小,再分配剩余空间(注意可能出现未均匀分配)
match_parent + weight无效或冲突,通常会撑满父容器,weight 失效

最佳实践:当使用 weight 时,主轴方向尽量设为 0dp


4. 垂直 LinearLayout 示例

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="上半部分" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:text="下半部分" />
</LinearLayout>

  • 上部分 : 下部分 = 1 : 2,按剩余高度分配

5. weightSum 属性

  • LinearLayout 可以使用 android:weightSum 指定 权重总和
  • 优点:
    • 保证按比例分配
    • 可以控制多控件权重占比和缩放范围
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3">

    <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="2"
        android:text="按钮2" />
</LinearLayout>

  • 与前面示例等价,但 明确总权重,更易维护

6. 注意事项

  1. 不要在非 LinearLayout 中使用 weight
  2. 过多嵌套 LinearLayout + weight 会影响性能 → 可用 ConstraintLayout + Guideline 替代
  3. ScrollView 中使用 weight → 需小心,可能撑开高度,导致布局异常
  4. wrap_content + weight 可能导致测量冲突,推荐使用 0dp

7. 常见坑

  • Button 设置 layout_width="match_parent" + weight → weight 被忽略
  • LinearLayout 水平 + 子 View 没设置 weight → 默认占用自身宽度,不会按比例分配
  • 嵌套 LinearLayout 多层使用 weight → 性能开销大

8. 实战技巧

  • 快速平分屏幕<LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <View android:layout_width="0dp" android:layout_height="2dp" android:layout_weight="1" android:background="#f00"/> <View android:layout_width="0dp" android:layout_height="2dp" android:layout_weight="1" android:background="#0f0"/> <View android:layout_width="0dp" android:layout_height="2dp" android:layout_weight="1" android:background="#00f"/> </LinearLayout>
  • 混合 wrap_content + weight:内容显示 + 剩余空间分配
  • 结合 weightSum:更精确控制比例