ConstraintLayout使用链布局时所有控件跳转到顶部问题求助
问题根因
你遇到的问题核心是垂直方向出现循环约束,导致ConstraintLayout无法正常计算控件位置。
你给text1添加了app:layout_constraintTop_toBottomOf="@id/button",同时给button添加了app:layout_constraintBottom_toTopOf="@id/text1",两个控件的垂直位置互相依赖,也没有给这条垂直链设置明确的定位锚点规则,ConstraintLayout解析约束失败后,会把所有位置计算异常的控件默认放到布局顶部,就出现了你现在的异常效果。
修复方案
你只需要删掉text1中多余的顶部约束即可,也就是删除app:layout_constraintTop_toBottomOf="@id/button"这一行。修改后的text1代码如下:
<TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is a" android:textSize="20sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/text3" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintStart_toStartOf="parent"/>
修改后逻辑说明
- text1底部绑定父布局底部,位置固定在布局底部
- button底部绑定text1顶部,自然就会固定在text1的上方位置
- 其余原有约束保持不变,即可实现你预期的布局效果
内容的提问来源于stack exchange,提问作者Sharas
相关产品推荐
相关产品推荐

