老婆保佑,代码无BUG
前言
Material Design 系列第三篇 FloatingActionButton
目录
- 一:使用
- xml
- activity
- 二:参数详解
- 三:与CoordinatorLayout 一起使用
引用
compile 'com.android.support:design:26.1.0'
Untitled.gif
FloatingActionButton是继承至ImageView,所以FloatingActionButton拥有ImageView的所有属性。CoordinatorLayout可以用来配合FloatingActionButton浮动按钮,设置app:layout_anchor和app:layout_anchorGravity构建出特定的位置与效果的FloatingActionButton。
一:使用
1. xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="bottom"
android:orientation="vertical">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:layout_margin="16dp"
android:src="@mipmap/ic_launcher"
app:backgroundTint="#30469b"
app:borderWidth="0dp"
app:elevation="6dp"
app:fabSize="normal"
app:layout_anchorGravity="bottom|right"
app:pressedTranslationZ="12dp"
app:rippleColor="#a6a6a6" />
</LinearLayout>
2. activity
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(v, "touch", Snackbar.LENGTH_SHORT).show();
}
});
二:参数详解
参数 | 说明 |
---|---|
app:backgroundTint | 设置FAB的背景颜色。 |
app:rippleColor | 设置FAB点击时的背景颜色。 |
app:borderWidth | 该属性尤为重要,如果不设置0dp,那么在4.1的sdk上FAB会显示为正方形,而且在5.0以后的sdk没有阴影效果。所以设置为borderWidth="0dp"。 |
app:elevation | 默认状态下FAB的阴影大小。 |
app:pressedTranslationZ | 点击时候FAB的阴影大小。 |
app:fabSize | 设置FAB的大小,该属性有两个值,分别为normal和mini,对应的FAB大小分别为56dp和40dp。 |
src | 设置FAB的图标,Google建议符合Design设计的该图标大小为24dp。 |
app:layout_anchor | 设置FAB的锚点,即以哪个控件为参照点设置位置。 |
app:layout_anchorGravity | 设置FAB相对锚点的位置,值有 bottom、center、right、left、top等。 |
三:与CoordinatorLayout 一起使用
Untitled.gif
会让FloatingActionButton浮动起来
实现
1。 修改根布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/CoordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:layout_margin="16dp"
android:src="@mipmap/ic_launcher"
app:backgroundTint="#30469b"
app:borderWidth="0dp"
app:elevation="6dp"
app:fabSize="normal"
app:layout_anchorGravity="bottom|right"
app:pressedTranslationZ="12dp"
app:rippleColor="#a6a6a6" />
</android.support.design.widget.CoordinatorLayout>
2. 修改SnackBar
Snackbar.make(findViewById(R.id.CoordinatorLayout), "touch", Snackbar.LENGTH_SHORT).show();
源码地址
点击进入GitHub
最后
全部系列