gpt4 book ai didi

android - 在 ConstraintLayout 的另一个 View 下水平居中两个 View

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:20:55 27 4
gpt4 key购买 nike

我有 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>

结果是取消按钮被按下,而不是与删除按钮水平对齐。 enter image description here

玩了一会儿之后,为了使取消按钮与删除按钮水平对齐,我不得不将以下任一添加到取消按钮。

app:layout_constraintBaseline_toBaselineOf="@+id/btn_delete"

app:layout_constraintVertical_bias="0.0"

问题:

  1. 为什么 ConstraintLayout 按下取消按钮而不是将其与删除按钮对齐?为什么我必须在取消按钮上使用基线或偏差才能使其对齐在同一行上?

  2. 除了使用baseline和bias之外,还有什么方法可以让取消按钮和删除按钮对齐吗?

最佳答案

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 按钮那样被按下呢? . 原因是,您在保存按钮上设置了以下约束

app:layout_constraintBottom_toTopOf="@+id/btn_delete"

如果你把它改成

app:layout_constraintBottom_toTopOf="@+id/btn_cancel"

delete 按钮将被按下,cancel 按钮将与 save 按钮对齐。

如果你把它改成

app:layout_constraintBottom_toBottomOf="parent"

你会得到如下布局

enter image description here

请注意,deletecancel 按钮现在以相同方式对齐。原因是现在 delete 和 cancel 按钮都有以下约束

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" 

取消删除按钮和改变

app:layout_constraintBottom_toTopOf="@+id/btn_delete"

app:layout_constraintBottom_toBottomOf="parent"

save 按钮布局中。

这将为您提供以下布局

enter image description here

此方法有效,因为删除 bottom constraint 会使 canceldelete 按钮都不垂直居中对齐 save 按钮和父布局底部之间的空间。

关于android - 在 ConstraintLayout 的另一个 View 下水平居中两个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48656532/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com