MD组件ExtendedFloatingActionButton

以下统一将ExtendedFloatingActionButton缩写为efab

efab结合CollapsingToolbarLayout

使用

一、配置repositories

在repositories下

allprojects {
    repositories {
      google()
      jcenter()
    }
  }

添加依赖

dependencies {
    // ...
    implementation 'com.google.android.material:material:1.1.0-alpha08'
    // ...
  }

1.1.0-alpha08是目前最新的版本,里面包含了efab(1.0.0版本不包含EFAB)
更多版本:MVN Repository

二、布局文件

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <!-- Main content -->

  <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
      android:id="@+id/efab"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="8dp"
      android:contentDescription="@string/extended_fab_content_desc"
      android:text="@string/extended_fab_label"
      android:theme="@style/Theme.MaterialComponents"
      app:icon="@drawable/ic_plus_24px"
      app:layout_anchor="@id/app_bar"
      app:layout_anchorGravity="bottom|right|end"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
注意点
1、运行时报引入布局错误

第一次使用时除了text的新特性外和fab没太大区别,于是没有注意theme属性的设置,结果在出现了引入布局的异常,无法引入efab,报错如下

Caused by: android.view.InflateException: Binary XML file line #73: Error inflating class com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).

根据报错信息可以发现,包含efab的父布局或efab本身的theme属性必须是material库提供的Theme.MaterialComponents或其子style

2、behavior的无法解析

目前我使用的最新版本1.1.0-alpha08的material库(其它版本未知,之前一直在用1.0.0)出现类似的引用就会无法解析,之前的1.0.0没有出现过这样的错误

app:layout_behavior="@string/appbar_scrolling_view_behavior"

解决方法,换成

app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"

同理,efab的behavior

app:layout_behavior="com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton$ExtendedFloatingActionButtonBehavior"

其实就是把需要到的behavior那个类引用过来...
当然,也可以引用自己定义的behavior类

3、属性名与fab的一些差别

efab的一些影响效果与fab类似的属性名字可能会不相同,比如

app:srcCompat  //fab
app:icon  //efab

EFAB的更多属性与style文件中item对应EFAB的属性见Material组件的ExtendedFloatingActionButton
)

三、带动画的隐藏和显示EFAB(这没什么好说的)

efab.show()  //显示
efab.hide()  //隐藏

四、展开和收起文本

//如果efab没有设icon或者text属性的其中一个,这两个方法都不会生效
efab.extend()  //展开
efab.shrink()  //收起

五、关于Behavior

在结合CollapsingToolbarLayout,AppbarLayout,CoordinatorLayout使用的时候,efab的默认behavior与fab的有所不同(废话),见开头的gif

简单看一下efab的Behavior的源码

protected static class ExtendedFloatingActionButtonBehavior<
          T extends ExtendedFloatingActionButton>
      extends CoordinatorLayout.Behavior<T> {
    private static final boolean AUTO_HIDE_DEFAULT = false;
    private static final boolean AUTO_SHRINK_DEFAULT = true;

    //...
    public ExtendedFloatingActionButtonBehavior() {
      super();
      autoHideEnabled = AUTO_HIDE_DEFAULT;
      autoShrinkEnabled = AUTO_SHRINK_DEFAULT;
    }
  //...
}

通过实际运行和粗略看一下源码
可以发现AUTO_HIDE_DEFAULT 和 AUTO_SHRINK_DEFAULT控制了efab在向上移动一段距离后的动作
而通过取值可知,efab将会收起文本,但不被隐藏,将会以icon的形式被挂在Toolbar上面
而且这个类是protected的,里面还有用到的一些类和方法都是被标上了这个库专用的注解,所以不可被继承,如果要实现别的behavior需要自己自定义behavior类

更多参考链接Material Design EFAB

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。