Android LinearLayout设置layout_weight后权重分配异常
问题场景
目标实现垂直方向按比例分配的三段式LinearLayout布局:
- header、footer区块各占总视图高度的10%
- body区块占据剩余80%高度
配置根布局android:weightSum="100",给header、footer设置android:layout_weight="10",body设置android:layout_weight="80"后,实际渲染效果和预期完全相反:权重更高的body直接消失,header和footer占满全部可用空间。
故障原因
核心错误是参与权重分配的三个子LinearLayout都设置了android:layout_height="match_parent"。
LinearLayout权重分配的计算逻辑是:
- 第一步先按子View声明的固定尺寸(垂直布局下取
layout_height值,水平布局下取layout_width值)计算所有子View的初始占用空间 - 第二步用父布局总尺寸减去第一步算出的初始总占用尺寸,得到剩余可分配空间
- 第三步把剩余空间按照各子View的
layout_weight比例,额外分配给对应子View
当三个子View高度都设为match_parent时,第一步计算的初始总高度需求已经是父布局总高度的3倍,没有任何剩余空间可用于权重分配,系统按布局渲染顺序先绘制的header、footer直接占满空间,后绘制的body就被完全挤压消失。
修复方案
垂直方向使用layout_weight分配高度时,将所有参与权重分配的子View的android:layout_height设为0dp即可;如果是水平方向分配宽度,对应将子View的android:layout_width设为0dp。这种写法会让系统跳过初始尺寸计算步骤,直接把对应方向的全部空间按权重比例分配给子View,完全符合比例分配的需求。
你只需要修改三个子LinearLayout的高度属性,内部原有代码不需要改动:
- 将header的
android:layout_height="match_parent"修改为android:layout_height="0dp" - 将body的
android:layout_height="match_parent"修改为android:layout_height="0dp" - 将footer的
android:layout_height="match_parent"修改为android:layout_height="0dp"
修正后的核心布局代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:weightSum="100"> <LinearLayout android:id="@+id/header" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="10" android:baselineAligned="false" android:orientation="horizontal"> <RelativeLayout android:id="@+id/head" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="5dp" android:background="@drawable/hf" android:padding="5dp"> <TextView android:id="@+id/texthead" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="Sample head" /> <ImageView android:id="@+id/imageh" android:layout_width="match_parent" android:layout_height="match_parent" app:srcCompat="@drawable/him" /> </RelativeLayout> </LinearLayout> <LinearLayout android:id="@+id/body" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="80" android:baselineAligned="false" android:orientation="horizontal"> <RelativeLayout android:id="@+id/bd0" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="5dp" android:background="@drawable/bdf" android:padding="10dp"> <TextView android:id="@+id/textbd0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="1dp" android:gravity="center" android:text="Lorem ipsum" /> <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="match_parent" app:srcCompat="@drawable/bdim" /> </RelativeLayout> <RelativeLayout android:id="@+id/bd1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="5dp" android:background="@drawable/bdf" android:padding="10dp"> <TextView android:id="@+id/textbd1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="1dp" android:gravity="center" android:text="Lorem ipsum" /> <ImageView android:id="@+id/imageView2" android:layout_width="match_parent" android:layout_height="match_parent" app:srcCompat="@drawable/bdim" /> </RelativeLayout> </LinearLayout> <LinearLayout android:id="@+id/footer" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="10" android:baselineAligned="false" android:orientation="horizontal"> <RelativeLayout android:id="@+id/ft" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="5dp" android:background="@drawable/ftf" android:padding="5dp"> <TextView android:id="@+id/textft" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="Sample footer" /> <ImageView android:id="@+id/imageft" android:layout_width="match_parent" android:layout_height="match_parent" app:srcCompat="@drawable/ftim" /> </RelativeLayout> </LinearLayout> </LinearLayout>
额外提示:当前body内部两个水平排列的RelativeLayout也都设置了
layout_width="match_parent",如果后续需要给这两个子块做水平比例分配,同样要把对应layout_width改为0dp再配置权重值,否则也会出现布局挤压的问题。
内容的提问来源于stack exchange,提问作者AndreaF
相关产品推荐
相关产品推荐

