在 ConstraintLayout 布局中,如果子视图的垂直约束不够完整,就会造成子视图向顶部偏移。可以添加一些垂直约束,比如设置子视图的底部或顶部相对于父视图底部或顶部的位置。下面是一个示例代码:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
在上面的示例中,Button 的底部、左侧、右侧和顶部都被约束到父视图。这样,就可以确保 Button 不会被向顶部偏移。