Java Android L-浮动操作按钮(FAB)

Java Android L-浮动操作按钮(FAB),java,android,android-5.0-lollipop,material-design,android-design-library,Java,Android,Android 5.0 Lollipop,Material Design,Android Design Library,谷歌是否已经为这个新的圆形晶圆厂按钮发布了定义的样式或组件,或者我应该自己实现这个设计 按钮如下所述: 编辑(2015年5月):检查/显示使用设计支持库实现它的简单方法。更新:现在有了FAB的官方小部件:FloatingActionButton,有关完整信息,请参阅Gabriele Mariotti回复 根据Adam Powell和Chet Haase的说法,他们没有为FAB按钮创建小部件,因为它是一个很容易复制的组件 谷歌IO 2014年的演讲“谷歌I/O 2014-材料科学:用材料设计开发A

谷歌是否已经为这个新的圆形晶圆厂按钮发布了定义的样式或组件,或者我应该自己实现这个设计

按钮如下所述:


编辑(2015年5月):检查/显示使用设计支持库实现它的简单方法。更新:现在有了FAB的官方小部件:FloatingActionButton,有关完整信息,请参阅Gabriele Mariotti回复

根据Adam Powell和Chet Haase的说法,他们没有为FAB按钮创建小部件,因为它是一个很容易复制的组件

谷歌IO 2014年的演讲“谷歌I/O 2014-材料科学:用材料设计开发Android应用程序”中有一个问题,在演讲结束时(大约37:50),你可以在这里听到这个问题:

Chet Haase说有一个RoundedBitmapDrawable(我没有检查它是不是名字)应该已经完成了定义大纲的工作

但是,您可以使用自己的drawable进行绘制,为其设置高程,并通过编程方式定义圆轮廓

这应该给你一个圆形按钮,在L上有阴影。 但我认为你必须自己建造Shadow pre-L

我应该检查CardView的代码,看看它是如何重现阴影的。我可能会这样做,但现在没有时间。如果没有人告诉我细节,我会在找到时间去检查后再做

编辑:

Gabriele Mariotti(参见下面的答案,谢谢)添加了一些代码,向您展示如何操作

感谢@shomeser的评论,他编写了一个库来制作fab按钮:

要使用它:

dependencies {
    compile 'com.shamanland:fab:0.0.3'
}

您还可以阅读他对另一个问题的回答:

更新日期:2019年8月16日,android library的官方材料组件

将新添加到您的
build.gradle

implementation 'com.google.android.material:material:1.0.0'
然后在布局中添加:

<com.google.android.material.floatingactionbutton.FloatingActionButton
      android:id="@+id/floating_action_button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="bottom|right"
      android:layout_margin="16dp"
      app:srcCompat="@drawable/ic_plus_24"/>
<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:src="@drawable/ic_done" />
如果您使用的是
Theme.MaterialComponents
等材质主题,则您的晶圆厂将继承该材质样式。否则只需应用style
@style/Widget.MaterialComponents.FloatingActionButton

<com.google.android.material.floatingactionbutton.FloatingActionButton
    ....
    style="@style/Widget.MaterialComponents.FloatingActionButton"
    ..../>
将此视图添加到布局中:

<com.google.android.material.floatingactionbutton.FloatingActionButton
      android:id="@+id/floating_action_button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="bottom|right"
      android:layout_margin="16dp"
      app:srcCompat="@drawable/ic_plus_24"/>
<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:src="@drawable/ic_done" />
文档:
更新日期:2014年12月2日,Android 5代码

您还可以向按钮添加和stateListAnimator

<Button
 android:stateListAnimator="@anim/anim"
/>

自从标签制作功能(如Evernote或收件箱应用程序)被添加到这一令人敬畏的功能中后,您可以随意使用它:

渐变依赖性:

    compile 'com.getbase:floatingactionbutton:1.3.0'
compile 'com.android.support:design:22.2.0'
Layout.xml:

     <com.getbase.floatingactionbutton.FloatingActionsMenu
        android:id="@+id/multiple_actions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        fab:fab_addButtonColorNormal="@color/white"
        fab:fab_addButtonColorPressed="@color/white_pressed"
        fab:fab_addButtonPlusIconColor="@color/half_black"
        fab:fab_labelStyle="@style/menu_labels_style"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp">

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            fab:fab_colorNormal="@color/white"
            fab:fab_title="Action A"
            fab:fab_colorPressed="@color/white_pressed"/>

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            fab:fab_colorNormal="@color/white"
            fab:fab_title="Action B"
            fab:fab_colorPressed="@color/white_pressed"/>

    </com.getbase.floatingactionbutton.FloatingActionsMenu>

菜单\u标签\u style.xml:

    <style name="menu_labels_style">
        <item name="android:background">@drawable/fab_label_background</item>
        <item name="android:textColor">@color/white</item>
    </style>

@可绘制/制作标签\u背景
@颜色/白色
fab_label_background.xml:

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/black_semi_transparent"/>
    <padding
        android:left="16dp"
        android:top="4dp"
        android:right="16dp"
        android:bottom="4dp"/>
    <corners
        android:radius="2dp"/>
</shape>


享受吧

谷歌现在提供了一个名为design library的官方库,其中包含Fab按钮。只需添加以下渐变依赖项:

    compile 'com.getbase:floatingactionbutton:1.3.0'
compile 'com.android.support:design:22.2.0'
之后,您可以像这样使用fab按钮:

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

或者在javadoc页面上


谢谢!与此同时,我在安卓l上的表现越来越好。但我有一个问题,这个“状态列表动画师”是做什么的?我实现了它,但实际上没有看到任何变化,是为了定制“涟漪”效果吗?@theyanu stateListAnimator根据按钮状态提供Z动画。仅供参考,如果使用
stateListAnimator
调整Z平移(如本答案中所示),则无需指定
高程。在这种情况下,添加一个
立面将使动画不那么明显,因为无论按钮处于何种状态(至少在Android-L的预览版本中),创建的阴影始终可见。@GabrieleMariotti似乎没有调用任何方法setOutline@DineshAnandan此方法已在android预览中使用。现在代码不同了。我已经用新代码更新了答案。我如何才能在这个按钮上添加材质选择效果?@Sandra当背景属性设置为“ripple”类型的绘图时,这个效果将自动添加。啊哈,我不知道,thx。我想我应该在哪里创建ripple.xml,drawables…好的,我让它工作了,thx给我指了指:)没有setOutline()这样的方法…虽然这个小部件非常简单,但我用这个小部件发布了库:
com.shamanland:fab:0.0.3
,查看我的帖子:这太棒了:)谢谢分享@罗曼:我只使用这个图书馆。但是,我不能用你在这里建议的方法得到涟漪。请提供一些意见。
    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/black_semi_transparent"/>
    <padding
        android:left="16dp"
        android:top="4dp"
        android:right="16dp"
        android:bottom="4dp"/>
    <corners
        android:radius="2dp"/>
</shape>
compile 'com.android.support:design:22.2.0'
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>