【问题标题】:button alignment left start and right end android kotlin按钮对齐左开始和右结束android kotlin
【发布时间】:2021-08-16 07:00:06
【问题描述】:
我想设计一个预期输出应该是这样的布局:
但我明白了
这是我的示例代码。知道如何修复它
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:gravity="left"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How"
android:gravity="end"/>
</LinearLayout>
【问题讨论】:
-
它的 layout_gravity 不是 gravity 。请改用ConstrainsLayout 或RelativeLayout。即使是FrameLayout 也可以。还有Check this.
标签:
android
android-layout
【解决方案1】:
用两个不同的 Android 布局试试下面的代码
使用相对布局
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentStart="true"
android:text="hello"
/>
<Button
android:layout_width="wrap_content"
android:layout_alignParentEnd="true"
android:layout_height="wrap_content"
android:text="How"
android:gravity="center"/>
</RelativeLayout>
使用约束布局
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:gravity="center"
android:text="hello"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:gravity="center"
android:text="How"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
【解决方案2】:
将您的父布局更改为相对布局
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:layout_alignParentStart="true"
android:layout_gravity="center"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
【解决方案3】:
为了正确实现ConstraintLayout,您可以遵循以下方法:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
app:layout_constraintEnd_toStartOf="@id/how"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/how"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/hello"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
【解决方案4】:
如果你想坚持使用LinearLayout,只需在how按钮的上方放置一个视图,如下所示:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="hello"
/>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How"
android:layout_gravity="center"/>