【问题标题】:How use the real Floating Action Button (FAB) Extended?如何使用真正的浮动操作按钮 (FAB) 扩展?
【发布时间】:2018-10-25 13:35:51
【问题描述】:

为了消除关于重复的任何疑虑或想法:Material Design 定义了“扩展”。

但大多数人将“扩展”与“Type of Transition: Speed Dial”混淆,这使得很难找到解决方案。赞here

问题 所以我期待的是如何使用文本和扩展大小设置 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 的扩展 FAB 尚不可用。也许尝试使用自定义视图来近似效果。
  • @LeoAso 但这只是一个例子,对吧?我得到的是他们还没有制作代码示例,但它已经存在......
  • 我不这么认为。该页面似乎链接到组件的实际实现,而不仅仅是示例。如果您检查标记为“代码可用”的链接,您将看到组件的文档。如果您点击 Android 链接,您会看到它尚未实现。
  • 它刚刚按计划添加到Material components Android library中,您可以在Github中看到提出的问题-github.com/material-components/material-components-android/…

标签: java android floating-action-button


【解决方案1】:
  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" />

【讨论】:

  • 给这个人一枚勋章!!
  • 不要忘记放置材质组件主题,例如'Theme.MaterialComponents.NoActionBar'
  • 太棒了!谢谢!
  • 如果您的活动主题不继承 'Theme.MaterialComponents.NoActionBar' ,则必须包含 android:theme="@style/Theme.MaterialComponents.NoActionBar" 作为按钮的属性。否则有机会获得布局膨胀异常
  • 这似乎不适用于材料 v 1.1.0(无 alpha)或更新版本(我试过 v 1.2.1)。为什么我需要1.1.0-alpha04 才能工作?有没有什么地方可以验证/检查是否有我可以使用的更新的实现包括这个?
【解决方案2】:

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

您可以阅读有关它的媒体帖子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);
}

【讨论】:

  • 我看到了,但是当出现snackBar 消息时,它的行为如何?它向上移动?掩盖信息还是被掩盖?
  • @Canato FAB 将像在支持库中一样向上移动
  • 我没有明白的一点是为什么你不简单地使用材料设计库的 FlootingActonButtonExtended
  • @FARID 如果您注意答案的日期,您会发现尚未发布。
【解决方案3】:

您可以使用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" 的作用类似于 fab:

祝你好运。

【讨论】:

    【解决方案4】:

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

    我是怎么做到的:

    <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>
    

    【讨论】:

    • 我现在试过了,但对我不起作用。你能说一下你用的是什么尺寸吗?如果你有结果的图片?谢谢。
    • @Canato 这不是解决方案。 Android 不会让你像这个人那样为 FAB 定义自定义背景。他可能相信它会像其他观点一样适用于 FAB,但不是 :))
    • @FARID 谢谢,我没有把它作为解决方案,我什至说这对我不起作用
    • @Canatov我不是说你。我的意思是发布这个答案的人
    猜你喜欢
    • 2023-03-15
    • 2015-12-26
    • 2015-08-30
    • 2015-06-23
    • 2017-08-25
    • 2020-01-11
    • 2016-07-02
    相关资源
    最近更新 更多