s-h*_*ter 8 android android-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>
Run Code Online (Sandbox Code Playgroud)
在玩了一下之后,为了使取消按钮与删除按钮水平对齐,我必须在取消按钮中添加以下任一项.
app:layout_constraintBaseline_toBaselineOf="@+id/btn_delete"
Run Code Online (Sandbox Code Playgroud)
要么
app:layout_constraintVertical_bias="0.0"
Run Code Online (Sandbox Code Playgroud)
问题:
为什么ConstraintLayout按下取消按钮而不是将其与删除按钮对齐?为什么我必须在取消按钮上使用基线或偏差才能使其在同一条线上对齐?
除了使用基线和偏差之外,还有其他方法可以使取消按钮与删除按钮对齐吗?
为什么ConstraintLayout按下取消按钮而不是将其与删除按钮对齐?为什么我必须在“取消”按钮上使用基线或偏差才能使其在同一行上对齐?
cancel按钮被按下是因为当您在cancel按钮上设置以下2个约束时
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_save"
Run Code Online (Sandbox Code Playgroud)
该cancel按钮被垂直居中之间save按钮和父布局的底部。
现在您可能会问该delete按钮也有两个相同的约束,那么为什么不像cancel按钮那样被按下呢?原因是,您在save按钮上设置了以下约束
app:layout_constraintBottom_toTopOf="@+id/btn_delete"
Run Code Online (Sandbox Code Playgroud)
如果您将其更改为
app:layout_constraintBottom_toTopOf="@+id/btn_cancel"
Run Code Online (Sandbox Code Playgroud)
delete按钮将被按下,并且cancel按钮将与save按钮对齐。
如果您将其更改为
app:layout_constraintBottom_toBottomOf="parent"
Run Code Online (Sandbox Code Playgroud)
您将获得以下布局
请注意,delete和cancel按钮现在以相同的方式对齐。原因是现在delete和取消按钮都具有以下约束
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_save"
Run Code Online (Sandbox Code Playgroud)
两个按钮都垂直位于save按钮和父布局底部之间的中心位置。
除了使用基线和偏差之外,还有其他方法可以使“取消”按钮与“删除”按钮对齐吗?
有多种方法可以实现所需的布局
一种方法是删除
app:layout_constraintBottom_toBottomOf="parent"
Run Code Online (Sandbox Code Playgroud)
从cancel和delete按钮并更改
app:layout_constraintBottom_toTopOf="@+id/btn_delete"
Run Code Online (Sandbox Code Playgroud)
至
app:layout_constraintBottom_toBottomOf="parent"
Run Code Online (Sandbox Code Playgroud)
在save按钮布局中。
这将为您提供以下布局
此方法之所以有效,是因为删除后bottom constraint,cancel和delete按钮都不会在save按钮和父布局底部之间的空间中垂直居中对齐。
app:layout_constraintBottom_toBottomOf="parent"从中删除 btn_cancel
<?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>
Run Code Online (Sandbox Code Playgroud)
要么
删除
app:layout_constraintVertical_chainStyle="packed"以获取更多详细信息,请检查chainStyle https://developer.android.com/training/constraint-layout/index.html#constrain-chain
| 归档时间: |
|
| 查看次数: |
4245 次 |
| 最近记录: |