文章目录
效果图

关键代码
xml布局文件如下
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/ic_done"
app:elevation="8dp"
app:fabSize="mini"
android:contentDescription="fab" />
kotlin逻辑代码如下:
val fab = findViewById<FloatingActionButton>(R.id.fab)
fab.setOnClickListener { v: View ->
Snackbar.make(v, "I am Snackbar", Snackbar.LENGTH_LONG)
.setAction("retry") {
Toast.makeText(this, "click me", Toast.LENGTH_LONG).show()
}.show()
}
FloatingActionButton常见属性含义如下:
| 属性 | 含义 |
|---|---|
app:backgroundTint | 设置背景颜色 |
app:rippleColor | 设置点击时的背景颜色 |
app:elevation | 默认状态下的阴影大小 |
app:fabSize | 设置大小,normal对应大小为56dp,mini对应大小为40dp |
app:layout_anchor | 设置指定控件为参照点的位置 |
app:pressedTranslationZ | 设置点击时FAB的阴影大小 |
app:fabSize设置为mini时如下:

app:fabSize设置为normal时如下:

自定义大小如下:

源码地址
https://github.com/yurensan/MaterialDesignDemo










