Java 如何使用真正的浮动操作按钮 (FAB) 扩展?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50354930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:34:52  来源:igfitidea点击:

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

javaandroidfloating-action-button

提问by Canato

To remove any doubts or thoughts about duplicate: on Material Designis defined what is "extended".

消除对重复的任何疑问或想法:在Material Design上定义了什么是“扩展”。

FAB - Extended FAB

FAB - 扩展 FAB

But most of the people confuses "extended" with "Type of Transition: Speed Dial", what make hard to find solutions. Like here

但大多数人将“扩展”与“转换类型:快速拨号”混淆,很难找到解决方案。喜欢这里

FAB - Type of Transition: Speed Dial

FAB - 转换类型:快速拨号

QuestionSo what I'm looking forward is how setup the FAB with text and a extended size.

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

Today my code is like this:

今天我的代码是这样的:

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

But my button look like this:

但我的按钮看起来像这样:

enter image description here

在此处输入图片说明

No text and no right format. I'm using it in a Constraint Layout.

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

采纳答案by Alexander HD

  1. Add the dependencies in your Gradle file:
  1. 在 Gradle 文件中添加依赖项:
implementation 'com.google.android.material:material:1.1.0-alpha04'

in your xml file:

在您的 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" />

回答by Arthulia

The short answer: set the background to a rect with rounded corners, rather than an oval. Then set the dimension of the radius to half that of the height of the button.

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

How I actually did it:

我实际上是如何做到的:

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

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>

回答by Mahdi Moqadasi

You can use this libraryto do that. (I used another way) But I used a FancyButtonto create a round and awesome button in coordinator layout as below:

你可以使用这个库来做到这一点。(我使用了另一种方式)但是我使用了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" />

and result is:

结果是:

enter image description here

在此处输入图片说明

That can have icon in it (see FancyButton).

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

with app:layout_behavior="@string/fab_transformation_sheet_behavior" it acts like fab as below:

app:layout_behavior="@string/fab_transformation_sheet_behavior" 它的作用就像如下FAB:

enter image description here

在此处输入图片说明

Good luck.

祝你好运。

回答by Tunji_D

enter image description here

在此处输入图片说明

You can create a utility class that animates a MaterialButtonto an Extended FloatingActionButtonusing a ConstraintLayout. You'll first need to declare the two states of the MaterialButtonin xml, and then use the TransitionManagerto animate between them.

您可以创建一个动画一个实用工具类MaterialButton,以一个扩展FloatingActionButton使用ConstraintLayout。您首先需要MaterialButton在 xml 中声明 的两个状态,然后使用TransitionManager来在它们之间设置动画。

You can read a medium post about it here, in the meantime, I'll add bits of relevant code here.

你可以在这里阅读一篇关于它的中等文章,同时,我会在这里添加一些相关的代码。

Collapsed FAB state:

折叠的 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>

Extended FAB State:

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

Background Drawable:

背景可绘制:

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

And relevant Java Expansion code:

以及相关的 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);
}