在 ConstraintLayout 的另一个视图下水平居中两个视图
Centering two views horizontally under another view in ConstraintLayout
我有 3 个按钮,一个全宽按钮位于屏幕半宽的两个按钮之上。这是布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<Button
android:id="@+id/btn_save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Save"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/btn_delete"
app:layout_constraintVertical_chainStyle="packed"/>
<Button
android:id="@+id/btn_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Delete"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/btn_cancel"
app:layout_constraintTop_toBottomOf="@+id/btn_save" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Cancel"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/btn_delete"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_save"/>
</android.support.constraint.ConstraintLayout>
结果是取消按钮被按下,而不是与删除按钮水平对齐。
玩了一会儿之后,为了使取消按钮与删除按钮水平对齐,我不得不将以下任一添加到取消按钮。
app:layout_constraintBaseline_toBaselineOf="@+id/btn_delete"
或
app:layout_constraintVertical_bias="0.0"
问题:
为什么ConstraintLayout按下了取消按钮而不是与删除按钮对齐?为什么我必须在取消按钮上使用基线或偏差才能使其在同一行上对齐?
除了使用baseline和bias,还有什么方法可以让取消按钮和删除按钮对齐?
从 btn_cancel
中删除 app:layout_constraintBottom_toBottomOf="parent"
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<Button
android:id="@+id/btn_save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Save"
app:layout_constraintBottom_toTopOf="@+id/btn_delete"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<Button
android:id="@+id/btn_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Delete"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/btn_cancel"
app:layout_constraintTop_toBottomOf="@+id/btn_save" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Cancel"
app:layout_constraintLeft_toRightOf="@+id/btn_delete"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_save" />
</android.support.constraint.ConstraintLayout>
或
移除
app:layout_constraintVertical_chainStyle="packed"
更多详情请查看 chainStyle
https://developer.android.com/training/constraint-layout/index.html#constrain-chain
Why did the ConstraintLayout pushed down the cancel button instead of
aligning it with the delete button? Why do I have to use the baseline
or bias on the cancel button in order to make it aligned on the same
line?
cancel
按钮被按下是因为当您在 cancel
按钮上设置以下 2 个约束时
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_save"
cancel
按钮在 save
按钮和父布局底部之间垂直居中。
现在你可能会问 delete
按钮也有相同的两个约束,那为什么它不像cancel
按钮那样被按下呢?。 原因是,您在 save
按钮上设置了以下约束
app:layout_constraintBottom_toTopOf="@+id/btn_delete"
如果你把它改成
app:layout_constraintBottom_toTopOf="@+id/btn_cancel"
delete
按钮将被按下,cancel
按钮将与 save
按钮对齐。
如果你把它改成
app:layout_constraintBottom_toBottomOf="parent"
你会得到如下布局
请注意,delete
和 cancel
按钮现在 以相同的方式对齐 。原因是现在 delete
和取消按钮都有以下约束
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_save"
两个按钮在 save
按钮和父布局底部之间 垂直居中 。
Besides using the baseline and the bias, are there any other ways to
make the cancel button align with the delete button?
有多种方法可以实现您想要的布局
一种方法是删除
app:layout_constraintBottom_toBottomOf="parent"
从 cancel
和 delete
按钮并更改
app:layout_constraintBottom_toTopOf="@+id/btn_delete"
到
app:layout_constraintBottom_toBottomOf="parent"
在 save
按钮布局中。
这将为您提供以下布局
此方法有效,因为删除 bottom constraint
会使 cancel
和 delete
按钮 不垂直居中对齐 在 space 在 save
按钮和父布局底部之间。
我有 3 个按钮,一个全宽按钮位于屏幕半宽的两个按钮之上。这是布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<Button
android:id="@+id/btn_save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Save"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/btn_delete"
app:layout_constraintVertical_chainStyle="packed"/>
<Button
android:id="@+id/btn_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Delete"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/btn_cancel"
app:layout_constraintTop_toBottomOf="@+id/btn_save" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Cancel"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/btn_delete"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_save"/>
</android.support.constraint.ConstraintLayout>
结果是取消按钮被按下,而不是与删除按钮水平对齐。
玩了一会儿之后,为了使取消按钮与删除按钮水平对齐,我不得不将以下任一添加到取消按钮。
app:layout_constraintBaseline_toBaselineOf="@+id/btn_delete"
或
app:layout_constraintVertical_bias="0.0"
问题:
为什么ConstraintLayout按下了取消按钮而不是与删除按钮对齐?为什么我必须在取消按钮上使用基线或偏差才能使其在同一行上对齐?
除了使用baseline和bias,还有什么方法可以让取消按钮和删除按钮对齐?
从 btn_cancel
app:layout_constraintBottom_toBottomOf="parent"
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<Button
android:id="@+id/btn_save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Save"
app:layout_constraintBottom_toTopOf="@+id/btn_delete"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<Button
android:id="@+id/btn_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Delete"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/btn_cancel"
app:layout_constraintTop_toBottomOf="@+id/btn_save" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Cancel"
app:layout_constraintLeft_toRightOf="@+id/btn_delete"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_save" />
</android.support.constraint.ConstraintLayout>
或
移除
app:layout_constraintVertical_chainStyle="packed"
更多详情请查看 chainStyle
https://developer.android.com/training/constraint-layout/index.html#constrain-chain
Why did the ConstraintLayout pushed down the cancel button instead of aligning it with the delete button? Why do I have to use the baseline or bias on the cancel button in order to make it aligned on the same line?
cancel
按钮被按下是因为当您在 cancel
按钮上设置以下 2 个约束时
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_save"
cancel
按钮在 save
按钮和父布局底部之间垂直居中。
现在你可能会问 delete
按钮也有相同的两个约束,那为什么它不像cancel
按钮那样被按下呢?。 原因是,您在 save
按钮上设置了以下约束
app:layout_constraintBottom_toTopOf="@+id/btn_delete"
如果你把它改成
app:layout_constraintBottom_toTopOf="@+id/btn_cancel"
delete
按钮将被按下,cancel
按钮将与 save
按钮对齐。
如果你把它改成
app:layout_constraintBottom_toBottomOf="parent"
你会得到如下布局
请注意,delete
和 cancel
按钮现在 以相同的方式对齐 。原因是现在 delete
和取消按钮都有以下约束
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_save"
两个按钮在 save
按钮和父布局底部之间 垂直居中 。
Besides using the baseline and the bias, are there any other ways to make the cancel button align with the delete button?
有多种方法可以实现您想要的布局
一种方法是删除
app:layout_constraintBottom_toBottomOf="parent"
从 cancel
和 delete
按钮并更改
app:layout_constraintBottom_toTopOf="@+id/btn_delete"
到
app:layout_constraintBottom_toBottomOf="parent"
在 save
按钮布局中。
这将为您提供以下布局
此方法有效,因为删除 bottom constraint
会使 cancel
和 delete
按钮 不垂直居中对齐 在 space 在 save
按钮和父布局底部之间。