Android LinearLayout addView为何需重设ConstraintLayout的LayoutParams
问题现象
通过addView方法动态向LinearLayout添加子View时存在以下特殊表现:
- 待插入的ConstraintLayout布局根节点已将
layout_width、layout_height设置为match_parent,直接调用addView插入时布局参数不生效,必须手动调用setLayoutParams传入两个MATCH_PARENT参数才能正常铺满父容器 - 该问题仅在插入子View为ConstraintLayout时出现,若插入子View为LinearLayout则无需手动设置布局参数
相关代码
主布局(centralPanel为动态填充内容的容器)
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:background="@drawable/background" android:columnCount="3"> <!-- left --> <LinearLayout android:id="@+id/leftPanel" android:orientation="vertical" android:layout_column="0" android:layout_gravity="start" android:layout_height="match_parent" android:layout_columnWeight="1"> </LinearLayout> <!-- center --> <LinearLayout android:id="@+id/centralPanel" android:layout_width="0dp" android:layout_height="match_parent" android:layout_row="0" android:layout_column="1" android:layout_columnWeight="20" android:layout_gravity="fill" android:layout_marginHorizontal="0dp" android:layout_marginTop="2mm" android:layout_marginBottom="5mm" android:orientation="vertical" android:background="@color/colorPrimary"> </LinearLayout> <!-- right --> <RelativeLayout android:id="@+id/rightPanel" android:orientation="vertical" android:layout_column="2" android:layout_gravity="end" android:layout_height="match_parent" android:layout_columnWeight="1"> </RelativeLayout >
待插入的ConstraintLayout布局
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_marginRight="30dp" android:layout_marginBottom="10dp" android:background="@color/colorAccent"> <TextView style="@style/DescriptionTextView" android:id="@+id/mapTextView" android:text="@string/goToLocation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@id/mapQuestImageView" app:layout_constraintVertical_chainStyle="spread_inside" /> <com.alexvasilkov.gestures.views.GestureImageView android:id="@+id/mapQuestImageView" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintTop_toBottomOf="@id/mapTextView" app:layout_constraintBottom_toTopOf="@id/mapQuestButton" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" /> <Button android:id="@+id/mapQuestButton" style="@style/ContinueButtonStyle" android:onClick="simpleScreenNext" app:layout_constraintTop_toBottomOf="@id/mapQuestImageView" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
修复前异常代码
private void showCentralQuest() { LinearLayout centralPanel = findViewById(R.id.centralPanel); centralPanel.removeAllViews(); centralPanel.addView(this.questLayout); centralPanel.invalidate(); }
修复后正常代码
private void showCentralQuest() { LinearLayout centralPanel = findViewById(R.id.centralPanel); centralPanel.removeAllViews(); this.questLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); centralPanel.addView(this.questLayout); centralPanel.invalidate(); }
根本原因
核心逻辑围绕Android布局参数的生效规则展开:
- 所有
layout_开头的属性,解析权完全属于子View的直接父容器,和子View自身类型无关。比如你在ConstraintLayout根节点写的android:layout_width="match_parent",这个参数不是给ConstraintLayout自己用的,是给它的直接父容器用来计算子View尺寸的。 - LayoutParams类型和父容器强绑定:不同类型的ViewGroup对应不同的LayoutParams子类,比如LinearLayout对应
LinearLayout.LayoutParams,ConstraintLayout对应ConstraintLayout.LayoutParams,两者不通用。 - 你代码里的
questLayout是提前inflate出来的,大概率inflate时用了inflate(R.layout.xxx, null)的写法:这种写法下布局解析器不知道View未来要添加到什么类型的父容器中,不会正确解析根节点的layout_*属性,此时ConstraintLayout持有的LayoutParams要么为null,要么是不符合LinearLayout要求的类型。 - 调用
addView添加子View时,LinearLayout会先检查子View携带的LayoutParams是不是LinearLayout.LayoutParams类型:- 如果类型不匹配,会直接调用自身的
generateDefaultLayoutParams()方法生成默认参数替换原有参数,而LinearLayout的默认布局参数就是宽高均为WRAP_CONTENT,你之前在xml里写的match_parent配置会直接丢失,自然无法铺满容器。 - 如果类型匹配,会保留子View原有的布局参数配置。
- 如果类型不匹配,会直接调用自身的
- 插入LinearLayout作为子View时不需要手动设置,本质是这个LinearLayout子View在inflate时要么传入了正确的LinearLayout类型父容器参数,要么它自带的LayoutParams刚好是LinearLayout能识别的类型,不会触发默认参数替换,所以xml里配置的
match_parent可以正常生效。 - 你手动调用
setLayoutParams传入LinearLayout.LayoutParams的操作,本质是提前给子View设置了LinearLayout认可的正确类型布局参数,addView时不会被替换成默认的wrap_content,自然就能正常铺满父容器。
注:inflate布局时尽量不要传null作为父参数,正确写法是
inflate(R.layout.xxx, 父容器, false),这种写法下布局解析器会自动根据父容器类型解析根节点的layout_*属性,生成正确类型的LayoutParams,addView时就不会出现参数丢失的问题。
内容的提问来源于stack exchange,提问作者Grzanek
相关产品推荐
相关产品推荐

