2015年5月29日金曜日

Android Support library v22.2で追加されたFloatingActionButtonを使う

早朝のGoogle I/O 2015のKeynoteと共に最新のAndroid Support libraryがリリースされました。
注目すべきはdesignパッケージです。本格的にマテリアルデザインのコンポーネントが追加されています。

http://developer.android.com/tools/support-library/index.html

Libのインポート

Android StudioでのDesign Support Libraryの追加方法は以下を参照、ですがTYPO(5/29時点)がありますので注意が必要です。
http://developer.android.com/tools/support-library/features.html#design

    compile 'com.android.support:design:22.2.0'

FloatingActionButton

次のように、レイアウトから使用可能です。

    <android.support.design.widget.FloatingActionButton
            android:id="@+id/floating_action_button"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:src="@drawable/XXXX"
            android.support.design:backgroundTint="@color/fab_bg"
            android:contentDescription="@string/XXXX"/>
FABのカラーはデフォルト:colorAccentです。もしカラーを変更する場合はandroid.support.design:backgroundTint属性でカラーを指定します。


備考

Design Support Libraryはリリースされたばかりで、まだまだissueが沢山あります。
Lollipop端末でFABを確認したところ、Shadowが正しく表示されませんでした。
FAB doesn't have shadow on Lollipop https://code.google.com/p/android/issues/detail?id=175068 The FloatingActionButton has different margins on Lollipop and pre-Lollipop. https://code.google.com/p/android/issues/detail?id=175330
動作がおかしい場合は、issue trackerを確認することをお勧めします。