Two floating action buttons next to each other

前端 未结 12 1356
谎友^
谎友^ 2020-12-23 09:26

The material design documentation has an example of Google Maps showing two floating action buttons next to one another (actually, one above the other).

相关标签:
12条回答
  • 2020-12-23 10:10

    I can't believe no one has posted the correct answer.

    Wrap the buttons in a ViewGroup and apply the dodgeInsetEdges layout parameter so that the buttons move up with the bottom sheet. For the above use case, we can use a LinearLayout with the XML attribute app:layout_dodgeInsetEdges="bottom".

    Note that we can apply this to any view in a CoordinatorLayout.

    <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto" ... >
    
        <!-- We can use any ViewGroup here. LinearLayout is the
             obvious choice for the questioner's use case. -->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:orientation="vertical"
            app:layout_dodgeInsetEdges="bottom">
    
            <android.support.design.widget.FloatingActionButton
                android:id="@+id/upperFab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/fab_margin"
                android:src="@drawable/upper_fab_icon" />
    
            <android.support.design.widget.FloatingActionButton
                android:id="@+id/lowerFab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/lower_fab_icon" />
    
        </LinearLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    
    0 讨论(0)
  • 2020-12-23 10:10

    Two floating action buttons with 100dp space in between them is as follows:

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_gravity="top|end"
        android:layout_marginBottom="100dp"
        android:src="@android:drawable/ic_input_add"
        app:layout_anchor="@id/fab"
        app:layout_anchorGravity="top" />
    
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_delete />
    
    0 讨论(0)
  • 2020-12-23 10:12

    Here is my solution, just put an invisible fab between the two fabs, and works well with CoordinatorLayout.

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_check" />
    
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_invisible"
        android:layout_width="@dimen/fab_margin"
        android:layout_height="@dimen/fab_margin"
        android:layout_gravity="top|end"
        android:layout_margin="@dimen/fab_margin"
        android:visibility="invisible"
        app:layout_anchor="@id/fab"
        app:layout_anchorGravity="top" />
    
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_follow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_gps_fixed_follow"
        app:backgroundTint="@android:color/white"
        app:layout_anchor="@id/fab_invisible"
        app:layout_anchorGravity="top" />
    

    No Snackbar

    With Snackbar

    0 讨论(0)
  • 2020-12-23 10:15

    It is a combination of layout gravity and anchor gravity together with playing with the margins (of the anchored item) that can make it work, have a look at the xml below which I have used successfully:

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_info" />
    
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|end"
        android:layout_marginBottom="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp"
        android:src="@android:drawable/ic_media_play"
        app:layout_anchor="@id/fab"
        app:layout_anchorGravity="top" />
    

    0 讨论(0)
  • 2020-12-23 10:15

    A very simple way (that worked for me!) was to just put the two floating buttons in a vertical linear layout at the bottom/end of its parent.

        <LinearLayout
        android:layout_gravity="bottom|end"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginEnd="@dimen/fab_margin"
            app:srcCompat="@android:drawable/ic_menu_send" />
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            app:srcCompat="@android:drawable/ic_menu_send" />
    </LinearLayout>
    

    Just adjust the layout_margin attributes for the floating buttons to get the spacing as you want it.

    0 讨论(0)
  • 2020-12-23 10:16

    The accepted answer (at the time of writing this) - https://stackoverflow.com/a/33900363/4414887 worked only partially as the buttons were sticking together.

    I was unable to use any other provided answers as well. So I found a workaround.

    Add a view in between the 2 FABs like so -

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab2"
        android:src="@drawable/ic_delete_white_24dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|end"
        app:layout_anchor="@id/view"/>
    
    <View
        android:id="@+id/view"
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:layout_gravity="top|end"
        app:layout_anchor="@id/fab"
        />
    
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:src="@drawable/ic_done_white_24dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:backgroundTint="@color/colorPrimary"
        app:layout_anchor="@id/bar2" />
    
    0 讨论(0)
提交回复
热议问题