You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在LinearLayout中使两个FloatingActionButton均分宽度并排?

解决FloatingActionButton在LinearLayout中平分宽度的问题

嘿,这个问题我之前做项目时也碰到过,FloatingActionButton(简称FAB)是个有点特殊的控件,直接设置它的layout_width没法让它平分LinearLayout的空间,咱们来看看怎么解决:

方案一:用LinearLayout权重+容器包裹

因为FAB的实际大小是由app:fabSize或app:fabCustomSize控制的,直接给它加layout_weight不会生效。咱们可以给每个FAB套一个容器(比如FrameLayout),让容器来平分LinearLayout的宽度,再把FAB放在容器的中心:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp"> <!-- 可以加 padding 让按钮不贴边 -->

    <!-- 第一个FAB的容器,占一半宽度 -->
    <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="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            app:fabSize="normal"/> <!-- 控制FAB大小 -->
    </FrameLayout>

    <!-- 第二个FAB的容器,占另一半宽度 -->
    <FrameLayout
        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_gravity="center"
            app:fabSize="normal"/>
    </FrameLayout>

</LinearLayout>

这样设置后,两个容器会各占LinearLayout宽度的一半,FAB在容器里居中显示,视觉上就实现了你要的“各自占据一半宽度”的效果。

方案二:改用ConstraintLayout(更灵活)

如果你的布局允许,改用ConstraintLayout会更简洁,不用额外套容器,直接用约束和 guideline 来划分空间:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/submitBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:fabSize="normal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/mid_guideline"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <!-- 垂直 guideline 放在布局中间位置 -->
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/mid_guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/DeleteBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:fabSize="normal"
        app:layout_constraintStart_toEndOf="@+id/mid_guideline"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

这个方案里,垂直的guideline把布局分成左右各50%的区域,两个FAB分别被约束到各自的区域,自动居中显示,效果和方案一一样,但布局结构更清爽。

为什么直接设置layout_width="1"没用?

这里要提一下:FloatingActionButton的layout_width和layout_height属性其实是被它的内部逻辑忽略的,它的实际大小是由app:fabSize(可选值:normal、mini、auto)或者app:fabCustomSize(自定义尺寸)来控制的,所以你之前设置layout_width="1"完全不会起作用~

内容的提问来源于stack exchange,提问作者AmirMahdi K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 18:37:51