[使用LinearLayout在一行(水平)android studio中设置三个按钮

问题描述 投票:0回答:4

我正在使用一个Android应用进行学习。并面临一些问题。无法将按钮中心设置为两个。我已经设置了两个按钮现在我需要在这两个按钮的中间放置另一个按钮并且视图将类似于this

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="start"
        android:gravity="start">

        <Button
            android:id="@+id/dialog_positive_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Set"
            android:background="#dde5ad" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="end"
        android:gravity="end">

        <Button
            android:id="@+id/dialog_negative_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="No"
            android:background="#dde5ad" />
    </LinearLayout>

我确切需要这两个的中心。

android xml button android-linearlayout
4个回答
1
投票

使用layout_weight

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="3"
    android:orientation="horizontal">

    <Button
        android:id="@+id/dialog_positive_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#dde5ad"
        android:text="Set" />

    <Button
        android:id="@+id/dialog_neutral_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#dde5ad"
        android:layout_weight="1"
        android:text="your text" />

    <Button
        android:id="@+id/dialog_negative_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#dde5ad"
        android:layout_weight="1"
        android:text="No" />

</LinearLayout>

0
投票

您需要为此使用layout_weightlayout_gravity

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:layout_weight="1"
        android:gravity="start"
        android:orientation="vertical">

        <Button
            android:id="@+id/dialog_positive_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#dde5ad"
            android:text="Set" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#dde5ad"
            android:text="Set" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="end"
        android:gravity="end"
        android:orientation="vertical">

        <Button
            android:id="@+id/dialog_negative_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#dde5ad"
            android:text="No" />
    </LinearLayout>
</LinearLayout>

0
投票

您可以摆脱不必要的线性布局。如果您只需要连续添加三个按钮,则只能在水平LinearLayout上执行以下操作:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
    <Button
        android:id="@+id/dialog_positive_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set"
        android:background="#dde5ad"
        />

    <!-- this is your new Button -->
     <Button
        android:id="@+id/new_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set"
        android:background="#dde5ad"
        />

    <Button
        android:id="@+id/dialog_negative_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="No"
        android:background="#dde5ad"
        />
</LinearLayout>

还请注意,嵌套居间布局可能会影响您的性能。如果需要进行复杂的布局,请尝试使用constraint layout

希望有帮助,编码愉快!


0
投票

您是否尝试过这种方法?

<LinearLayout
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/dialog_positive_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Set"
            android:background="#dde5ad"
            />

        <Button
            android:layout_marginStart="20dp"
            android:id="@+id/center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Center"
            android:background="#dde5ad"
            />


        <Button
            android:layout_marginStart="20dp"
            android:id="@+id/dialog_negative_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="No"
            android:background="#dde5ad"
            />
    </LinearLayout>
© www.soinside.com 2019 - 2024. All rights reserved.