android 按钮并排左右_用户界面-并排2个按钮-android布局

在Android中实现两个按钮并排显示,可以使用水平线性布局或相对布局。通过设置布局权重和宽度,可以确保按钮平分屏幕宽度并保持间距。文章提供了不同解决方案,包括直接设置按钮宽度为match_parent和使用gravity属性来居中按钮。
摘要由CSDN通过智能技术生成

用户界面-并排2个按钮-android布局

我如何才能将2个按钮并排放置,以使它们占据整个宽度,并且它们之间有一点间距。

我想过一个水平线性布局,其中2个子线性布局设置为与父级和权重1匹配,每个线性布局都包含按钮..有没有更简单的方法? 可以通过相对布局完成吗?

谢谢!

4个解决方案

104 votes

android:id="@+id/LinearLayout02"

android:layout_height="wrap_content"

android:layout_width="match_parent"

android:layout_alignParentBottom="true">

android:id="@+id/Button02"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1" android:text="Apply">

android:id="@+id/Button03"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:lay

要在Android Studio实现并排按钮,您可以使用LinearLayout或ConstraintLayout布局来放置两个按钮。这里有两种常见的方法: 方法一:使用LinearLayout布局 您可以使用一个水平的LinearLayout来容纳两个按钮,并将它们并排放置。以下是一个示例代码片段: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button 1" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button 2" /> </LinearLayout> ``` 方法二:使用ConstraintLayout布局 您也可以使用ConstraintLayout来放置两个按钮。以下是一个示例代码片段: ```xml <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toStartOf="@+id/button2" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" app:layout_constraintStart_toEndOf="@+id/button1" app:layout_constraintEnd_toEndOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> ``` 这两种方法都可以实现并排按钮布局。您可以根据需要选择其一种布局方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值