如何将浮动操作按钮转换为具有相关操作的菜单[关闭]

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

如何将浮动操作按钮(FAB)转换为菜单,这是我的xml布局


    <android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.applandeo.materialcalendarview.CalendarView
    android:id="@+id/calendarView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:layout_weight="1"
    app:datePicker="true"
    app:eventsEnabled="true"
    app:layout_constraintBottom_toTopOf="@+id/floatingActionButton3"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">
    </com.applandeo.materialcalendarview.CalendarView>
    <android.support.design.widget.FloatingActionButton
    android:id="@+id/floatingActionButton3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:clickable="true"
    android:focusable="true"
    android:scaleType="center"
    android:tint="@color/blanco"
    app:backgroundTint="@color/colorAndroid"
    app:fabSize="normal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:srcCompat="@android:drawable/ic_input_add" />  
    </android.support.constraint.ConstraintLayout>
    </FrameLayout>

我想将按钮转换为相关的操作菜单 “转换为具有相关操作的菜单”,如材料设计所示: https://storage.googleapis.com/spec-host-backup/mio-design%2Fassets%2F1pdXG8K2i6IR9i5V5raflvDwuADXdUACM%2Ffab-transitions-menu-1.mp4

android menu floating-action-button android-transitions
1个回答
0
投票

android中有一个原生的浮动动作按钮,但现在没有可用的代码可以扩展到子按钮/动作。您可以通过在布局上添加多个FAB来模拟该效果,一个在另一个上面,当您单击顶部的那个时,您可以通过更改其屏幕坐标来动画其余的以展开:

这是FAB的简单布局:

<FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/fab4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:gravity="center_vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="text"/>

                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/fab4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="end|bottom"
                    android:layout_margin="16dp"
                    android:src="@drawable/ic_action_download"

                    app:fabSize="mini"
                    app:rippleColor="@color/download_in_progress"/>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/fab3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:gravity="center_vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="text"/>

                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/fab3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="end|bottom"
                    android:layout_margin="16dp"
                    android:src="@drawable/ic_action_download"

                    app:fabSize="mini"
                    app:rippleColor="@color/download_in_progress"/>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/fab2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:gravity="center_vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="text"/>

                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/fab2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="end|bottom"
                    android:layout_margin="16dp"
                    android:src="@drawable/ic_action_download"

                    app:fabSize="mini"
                    app:rippleColor="@color/download_in_progress"/>
            </LinearLayout>


            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:gravity="center_vertical">

                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/topFab"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="end|bottom"
                    android:layout_margin="16dp"
                    android:src="@drawable/ic_action_download"
                    app:backgroundTint="@color/white"
                    app:borderWidth="4dp"
                    app:elevation="12dp"
                    app:rippleColor="@color/download_in_progress"/>
            </LinearLayout>
        </FrameLayout>

然后:

//to expand the buttons:
topFab.animate().rotationBy(180);
fab2.animate().translationY(-150);
fab3.animate().translationY(-300);
fab4.animate().translationY(-450);

//to collapse them:
topFab.animate().rotationBy(-180);
fab2.animate().translationY(0);
fab3.animate().translationY(0);
fab4.animate().translationY(0);

您还可以切换其可见性,以便在设置动画时消除任何视觉延迟。

或者,您可以使用为您执行此操作的库。一个例子是Clans/FloatingActionButton

© www.soinside.com 2019 - 2024. All rights reserved.