Android按钮水平居中,android - ConstraintLayout:将两个按钮水平居中 - SO中文参考 - www.soinside.com...

博客内容涉及Android开发,作者在尝试使用ConstraintLayout来实现两个按钮的水平居中对齐,但遇到了问题。已经尝试了相关线程的解决方案以及调整了app:layout_constraintHorizontal_chainStyle属性,但未达到预期效果。XML代码片段展示了当前布局设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

虽然看似简单,但我做不到,但我希望两个按钮从侧面触摸并水平居中,就像这样:

eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9jVXRMSi5wbmcifQ==

我尝试了该线程中的答案:Center two buttons horizontally,但它仅与RelativeLayout有关,与ContrainstLayout有关

我也尝试过玩app:layout_constraintHorizontal_chainStyle="spread"

但是没有成功。我没有帮助的xml:<?xml version="1.0" encoding="utf-8"?>

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">

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" />

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" />

使用ConstraintLayout可以实现吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值