概述
Floating Action Button 簡(jiǎn)稱 FAB眉枕,是一個(gè)懸浮的圓形按鈕恶复,帶有陰影(MD 的設(shè)計(jì)思想就是呈現(xiàn)真實(shí)世界的效果怜森,是包含燈光和陰影的)。這個(gè)設(shè)計(jì)很棒谤牡!
FAB 的詳細(xì)介紹可以參照 https://material.io/guidelines/components/buttons-floating-action-button.html 副硅。
FAB 代碼詳解
首先需要引入 Design Support Library。
compile 'com.android.support:design:25.3.1'
Design Support Library 依賴于 v4 和 v7翅萤,所以不需要重復(fù)引入恐疲。
然后在 Layout 文件中加入 FAB。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.teletian.materialdesign.MainActivity"
tools:ignore="RtlHardcoded">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
android:src="@drawable/ic_add_white_24dp"
app:backgroundTint="@color/colorAccent"
app:elevation="6dp"
app:fabSize="normal"
app:pressedTranslationZ="12dp"
app:rippleColor="@color/colorPrimary" />
</RelativeLayout>
關(guān)于 FAB 的屬性套么,這里解釋一下
app:fabSize:FAB 的大小培己。
https://material.io/guidelines/components/buttons-floating-action-button.html# 這里面也有介紹,
Normal(Default): 56 x 56dp
Mini: 40 x 40dp
不管是 Normal(Default) 還是 Mini违诗,內(nèi)部的 icon 都一樣大漱凝,24 x 24dp。
app:elevation:FAB 的海拔高度诸迟。
app:pressedTranslationZ:FAB 的按下?tīng)顟B(tài)的海拔高度茸炒。
按下?tīng)顟B(tài)的海拔高度是高于正常情況下的海拔高度的。
默認(rèn)的 app:elevation 為 6dp阵苇。默認(rèn)的 app:pressedTranslationZ 為 12dp壁公。
各種默認(rèn)的海拔高度可以參照 https://material.io/guidelines/material-design/elevation-shadows.html# 。
app:backgroundTint:FAB 的顏色绅项。默認(rèn)情況下使用強(qiáng)調(diào)色紊册,可以更改為其他顏色。
app:rippleColor:FAB 的漣漪顏色快耿。Material Design 要求按鈕按下要有漣漪效果囊陡,默認(rèn)的漣漪顏色是灰色的,可以更改為其他的顏色掀亥。
FAB 還可以設(shè)置顯示和隱藏撞反。例如有些應(yīng)用要求滑動(dòng)時(shí)顯示和隱藏。
fab.show()
fab.hide()
設(shè)置顯示和隱藏的時(shí)候監(jiān)聽(tīng)結(jié)束搪花,然后做相應(yīng)的操作遏片。
fab.show(new FloatingActionButton.OnVisibilityChangedListener() {
@Override
public void onShown(FloatingActionButton fab) {
super.onShown(fab);
}
});
fab.hide(new FloatingActionButton.OnVisibilityChangedListener() {
@Override
public void onHidden(FloatingActionButton fab) {
super.onHidden(fab);
}
});
陰影不顯示的 Bug
在老版本的 Support Library 上,會(huì)有陰影不顯示的 Bug撮竿。
具體可以參照 https://issuetracker.google.com/issues/37048580 吮便。