一、介紹
這個類是繼承自ImageView的,所以對於這個控制項我們可以使用ImageView的所有屬性
android.support.design.widget.FloatingActionButton
二、使用準備, 在as 的 build.grade檔案中寫上
compile 'com.android.support:design:22.2.0'
三、使用說明
xml檔案中,注意藍色字型部分
<android.support.design.widget.FloatingActionButtonandroid:id="@+id/floatingActionButton"android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_centerInParent="true"android:src="@mipmap/ok"app:borderWidth="0dp"app:backgroundTint="#FF4011"app:rippleColor="#33728dff"app:elevation="8dp"app:pressedTranslationZ="16dp"/>
可以看到我們使用了app屬性,則需要在根容器中添加這個屬性。
xmlns:app="http://schemas.android.com/apk/res-auto"
屬性介紹:
1、app:borderWidth=""------------------邊框寬度,通常設定為0 ,用於解決Android 5.X裝置上陰影無法正常顯示的問題
2、app:backgroundTint=""---------------按鈕的背景顏色,不設定,預設使用theme中colorAccent的顏色
3、app:rippleColor=""--------------------點擊的邊緣陰影顏色
4、app:elevation=""----------------------邊緣陰影的寬度
5、app:pressedTranslationZ="16dp"-----點擊按鈕時,按鈕邊緣陰影的寬度,通常設定比elevation的數值大
另外我們希望點擊按鈕一個顏色,正常狀態一個顏色 以提高使用者體驗,那麼就是drawable檔案夾中建立檔案 floatbutton.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@color/colorNormal"></item><item android:state_pressed="true" android:drawable="@color/colorPressed"></item></selector>
然後我們設定FloatActionButton的backgroud為這個drawable檔案即可,同時app:backgroundTint=""屬性也可以不再設定
<android.support.design.widget.FloatingActionButtonandroid:id="@+id/floatingActionButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:background="@drawable/floatbutton"android:src="@mipmap/ok"app:borderWidth="0dp"app:rippleColor="#33728dff"app:elevation="8dp"app:pressedTranslationZ="16dp"/>
效果圖: