gpt4 book ai didi

android - 约束布局 : Center two buttons horizontally

转载 作者:行者123 更新时间:2023-12-03 18:17:21 25 4
gpt4 key购买 nike

虽然看起来很简单,但我做不到,但我希望两个按钮从侧面相互接触并水平居中,如下所示:

enter image description here

我在这个线程中尝试了答案:Center two buttons horizontally ,但它只与RelativeLayout有关,与ContrainstLayout无关

我也试过玩

app:layout_constraintHorizontal_chainStyle="spread"

但没有成功。我的无用 xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
style="@style/btnStyle"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:background="@color/btnTrue"
android:text="Button"
android:textColor="#ffffff"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button2"
style="@style/btnStyle"
android:layout_height="wrap_content"
android:layout_marginEnd="56dp"
android:background="@color/btnFalse"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
tools:layout_editor_absoluteY="0dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

这可以通过 ConstraintLayout 实现吗?

最佳答案

这应该做的工作。

<androidx.constraintlayout.widget.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/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/button"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

关键是删除不需要的参数并使用
 app:layout_constraintHorizontal_chainStyle="packed"

关于android - 约束布局 : Center two buttons horizontally,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58929948/

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