參考:http://blog.csdn.net/lmj623565791/article/details/46678867
添加依賴: compile 'com.android.support:design:22.2.0'
布局:
<android.support.design.widget.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right|bottom" android:src="@drawable/ic_discuss" />
- 填充色以及按下的顏色如何自定義呢?
按下的顏色是 rippleColor ,默認取的是主題中的 colorControlHighlight
填充色是 backgroundTint ,默認取的是主題中的 colorAccent (這個屬性好像要API21才能用)
注:colorAccent 對應EditText編輯時、RadioButton選中、CheckBox等選中時的顏色。
也可以直接用屬性定義這兩個的顏色:
app:backgroundTint="#ff87ffeb"
app:rippleColor="#33728dff"
- 立體感有沒有什么屬性可以動態指定?
和立體感相關有兩個屬性,elevation和pressedTranslationZ,前者用戶設置正常顯示的陰影大小;后者是點擊時顯示的陰影大小。
<android.support.design.widget.FloatingActionButton xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right|bottom" android:src="@drawable/ic_discuss" app:backgroundTint="#ff87ffeb" app:rippleColor="#33728dff" app:elevation="6dp" app:pressedTranslationZ="12dp" />
5.x存在的一些問題
在5.x的設備上運行,你會發現一些問題(測試系統5.0):
- 木有陰影
添加屬性 app:borderWidth="0dp"
- 對於5.x設置一個合理的margin,整體如下:
<android.support.design.widget.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end|bottom" app:borderWidth="0dp" android:layout_margin="@dimen/fab_margin" android:src="@drawable/ic_headset" />
values
<dimen name="fab_margin">0dp</dimen>
values-v21
<dimen name="fab_margin">16dp</dimen>
2015-06-29
17:35:25