【发布时间】:2020-03-14 17:32:23
【问题描述】:
虽然看起来很简单,但我做不到,但我希望两个按钮从侧面相互接触并水平居中,如下所示:
我在这个帖子中尝试了答案: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 实现吗?
【问题讨论】:
-
在水平线性布局中添加按钮并将容器在屏幕上居中。
标签: android android-constraintlayout