【问题标题】:How to align two FloatingActionButtons together in a LinearLayout?如何在 LinearLayout 中将两个 FloatingActionButtons 对齐?
【发布时间】:2020-05-19 10:31:13
【问题描述】:

所以我试图在LinearLayout 中对齐FloatingActionButton 中的两个RelativeLayout。 像这样的:

<LinearLayout>
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/submitBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/DeleteBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

但是当您将它们放在LinearLayout 中时,我希望它们的行为与TextView 之类的任何其他元素一样。 我的意思是我希望它们每个都占据LinearLayout 宽度的一半。 但是他们会坚持LinearLayout的起点,无论我怎么尝试,他们都不会移动。

我尝试在它们每个上都使用android:layout_width="1",但没有运气,它们根本不会移动,它们只是在LinearLayout 的开头粘在一起。

它的外观如下:

如果有任何建议,我会很高兴!提前致谢。

【问题讨论】:

  • 你想要一个浮动按钮在左侧,另一个在右侧正确吗?
  • @PraveenSP 是的,宽度均匀
  • 设置layout_weight 1 在他们和重力为Start。试试看
  • @LalitFauzdar 不,没用!

标签: android android-layout android-linearlayout floating-action-button


【解决方案1】:

实现它的一种方法是这样的......

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

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/submitBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            app:srcCompat="@drawable/ic_add_black_24dp" />

    </FrameLayout>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/deleteButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            app:srcCompat="@drawable/ic_delete_forever_black_24dp" />

    </FrameLayout>

</LinearLayout>

它看起来像这样......

您可以使用 layout_gravity 来根据需要对齐它们...

【讨论】:

  • 不是问题快乐编码... :)
【解决方案2】:

请在此 XML 代码下方使用

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="2"
    >
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
       >
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/submitBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="right"
        android:gravity="right"
        >
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/DeleteBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

【讨论】:

  • 这行得通,谢谢。但我希望两个按钮都位于中心,但这会使它们向右对齐。无论如何,我设法通过将内部 LinearLayouts' gravity 设置为 center 来使其工作。
【解决方案3】:

通过一些简单的数学运算,您可以与此代码完美对齐,只需复制粘贴:

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

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/submitBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/DeleteBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

    </RelativeLayout>

</LinearLayout>

完全居中,中间没有大间隙:

【讨论】:

    【解决方案4】:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal">
            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/submitBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
    
            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/DeleteBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
    </LinearLayout>
    

    【讨论】:

    • 正如我在问题中提到的,由于某种原因,更改 layout_weight 在这种情况下不起作用
    【解决方案5】:

    在 LinearLayout 中你可以设置 layout_weight=1 来共享父级:

     <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/submitBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
    

    【讨论】:

    • 我希望这能奏效,但正如我在问题中提到的那样,改变重量不起作用。
    • 必须是线性布局?它可以很容易地在约束布局中实现
    • 我认为 LinearLayout 在我的情况下是最好的布局,但谢谢我会检查它是否也有效。
    猜你喜欢
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    • 2014-01-10
    • 1970-01-01
    相关资源
    最近更新 更多