ConstraintLayout中include引入的按钮无法对齐父布局底部怎么解决?
问题解决方案
根因
你在<include>标签中未显式指定layout_width和layout_height属性,导致你为该标签配置的ConstraintLayout约束全部失效,系统会默认将引入的布局放置在父布局左上角,就是你看到的按钮显示在顶部的问题。
修复代码
仅需修改登录页面的include标签部分,补充宽高属性即可:
<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/black"> <include android:id="@+id/login_button" layout="@layout/widget_large_button" <!-- 新增以下两个宽高属性 --> android:layout_width="match_parent" android:layout_height="wrap_content" bind:buttonTitle="@{@string/login}" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"/> </androidx.constraintlayout.widget.ConstraintLayout>
注意事项
- 在ConstraintLayout中为
<include>标签添加约束时,必须显式声明layout_width和layout_height属性,即使被引入的布局根节点已经配置了相同的宽高,也需要在include标签中重复声明,约束才能正常生效。 - 你原有配置的
app:layout_constraintHorizontal_bias="0.0"属性在同时约束左右两边到父布局、且宽度为match_parent的场景下没有作用,可以直接删除。
内容的提问来源于stack exchange,提问作者Seb
相关产品推荐
相关产品推荐

