如何使用真正的 Floating Action Button (FAB) Extended?

How use the real Floating Action Button (FAB) Extended?

消除关于重复的任何疑虑或想法:在 Material Design 上定义了什么是 "extended"。

但大多数人将 "extended" 与“Type of Transition: Speed Dial", what make hard to find solutions. Like

混淆了

问题 所以我期待的是如何设置带有文本和扩展大小的 FAB。

今天我的代码是这样的:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/maps_main_distance_btn"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:text="@string/str_distance_btn"
    app:elevation="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

但是我的按钮是这样的:

没有文字,也没有正确的格式。我在约束布局中使用它。

简短的回答:将背景设置为带圆角的矩形,而不是椭圆形。然后将半径的尺寸设置为按钮高度的一半。

我是怎么做到的:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/maps_main_distance_btn"
    android:layout_width="@dimen/floating_button_width"
    android:layout_height="@dimen/floating_button_height"
    android:layout_marginBottom="8dp"
    android:background="@drawable/btn_background_expanded"
    android:text="@string/str_distance_btn"
    app:elevation="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

drawable/btn_background_expanded.xml:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="?android:colorControlHighlight">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="?android:colorAccent" />
            <corners
                android:radius="@dimen/floating_button_radius"
                />
        </shape>
    </item>
</ripple>

您可以使用 this library 来做到这一点。 (我用了另一种方式) 但是我使用 FancyButton 在协调器布局中创建了一个圆形的很棒的按钮,如下所示:

    <mehdi.sakout.fancybuttons.FancyButton
    android:id="@+id/actMain_btnCompare"
    app:layout_behavior="@string/fab_transformation_sheet_behavior"
    android:layout_width="125dp"
    android:layout_height="38dp"
    android:layout_gravity="bottom|center"
    android:layout_margin="20dp"
    android:elevation="3dp"
    android:padding="2dp"
    fancy:fb_borderColor="@color/white"
    fancy:fb_borderWidth="3dp"
    fancy:fb_defaultColor="@color/colorPrimaryDark"
    fancy:fb_radius="20dp"
    fancy:fb_text="مقایسه محصول"
    fancy:fb_textColor="@color/white"
    fancy:fb_textGravity="center"
    fancy:fb_textSize="13sp" />

结果是:

里面可以有图标(见FancyButton)。

app:layout_behavior="@string/fab_transformation_sheet_behavior" 它的表现如下所示:

祝你好运。

您可以创建一个实用程序 class,使用 ConstraintLayoutMaterialButton 动画化为扩展 FloatingActionButton。您首先需要在 xml 中声明 MaterialButton 的两个状态,然后使用 TransitionManager 在它们之间设置动画。

你可以阅读一个媒体 post 关于它 here,同时,我会在这里添加一些相关代码。

折叠的 FAB 状态:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout android:id="@+id/extend_fab_container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_fab"
    android:elevation="8dp">

    <android.support.design.button.MaterialButton
        android:id="@+id/fab"
        style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
        android:layout_width="56dp"
        android:layout_height="56dp"
        app:cornerRadius="56dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

扩展 FAB 状态:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout android:id="@+id/extend_fab_container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:elevation="8dp"
    android:background="@drawable/bg_fab">

    <android.support.design.button.MaterialButton
        android:id="@+id/fab"
        style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cornerRadius="56dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

可绘制的背景:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorAccent" />
    <corners android:radius="56dp" />
</shape>

及相关Java扩展码:

private void setExtended(boolean extended, boolean force) {
    if (isAnimating || (extended && isExtended() && !force)) return;

    ConstraintSet set = new ConstraintSet();
    set.clone(container.getContext(), extended ? R.layout.fab_extended : R.layout.fab_collapsed);

    TransitionManager.beginDelayedTransition(container, new AutoTransition()
            .addListener(listener).setDuration(150));

    if (extended) button.setText(currentText);
    else button.setText("");

    set.applyTo(container);
}
  1. 在您的 Gradle 文件中添加依赖项:
implementation 'com.google.android.material:material:1.1.0-alpha04'

在您的 xml 文件中:

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_margin="@dimen/fab_margin"
            android:text="Create"
            app:icon="@drawable/outline_home_24" />