帶展開菜單的浮動操作按鈕

Annabelle 8年前發布 | 43K 次閱讀 Android開發 移動開發

帶展開菜單的浮動操作按鈕,菜單item可以像標準menu那樣在xml里設置,還可以定義fab的位置。

使用說明:

開始

把依賴添加進gradle.build

dependencies {
    compile 'io.github.yavski:fab-speed-dial:1.0.1'
}

定義菜單資源文件

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_call"
        android:icon="@drawable/ic_call_black_24px"
        android:title="@string/menu_item_call" />
    <item
        android:id="@+id/action_text"
        android:icon="@drawable/ic_chat_bubble_outline_black_24px"
        android:title="@string/menu_item_text"/>
    <item
        android:id="@+id/action_email"
        android:icon="@drawable/ic_mail_outline_black_24px"
        android:title="@string/menu_item_email" />
</menu>

把FabSpeedDial添加到 layout xml

<FrameLayout 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:padding="16dp">

    <io.github.yavski.fabspeeddial.FabSpeedDial
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        app:fabGravity="bottom_end"
        app:fabMenu="@menu/menu_main"
        app:miniFabBackgroundTintColor="@android:color/white"
        app:miniFabImageTintColor="?attr/colorPrimaryDark"
        app:miniFabTitleTextColor="?attr/colorPrimaryDark" />

</FrameLayout>

效果

事件

跟所有的menu一樣,在item列表顯示之前有一個回調。這個回調允許你更新menu item

        FabSpeedDial fabSpeedDial = (FabSpeedDial) findViewById(R.id.fab_speed_dial);
        fabSpeedDial.setMenuListener(new SimpleMenuListenerAdapter() {
            @Override
            public boolean onPrepareMenu(NavigationMenu navigationMenu) {
                // TODO: Do something with yout menu items, or return false if you don't want to show them
                return true;
            }
        });

類似的,在item被選擇的時候,我們有:

        FabSpeedDial fabSpeedDial = (FabSpeedDial) findViewById(R.id.fab_speed_dial);
        fabSpeedDial.setMenuListener(new SimpleMenuListenerAdapter() {
            @Override
            public boolean onMenuItemSelected(MenuItem menuItem) {
               //TODO: Start some activity
              return false;
            }
        });

自定義

位置

為了改變view的位置,使用標準的android API去定義FabSpeedDial在ViewGroup中的位置,即設置fabGravity的值:

<FrameLayout
    android:id="@+id/content_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize">

    <io.github.yavski.fabspeeddial.FabSpeedDial
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        app:fabMenu="@menu/menu_main"
        app:fabGravity="bottom_end" />


  

基本樣式

支持以下屬性:

FabSpeedDial Android Desscription
app:fabDrawable android:src Sets the icon drawable of the main FAB
app:fabDrawableTint android:tint Tints the icon drawable of the main FAB
app:fabBackgroundTint android:backgroundTint Tints the background colour of the main FAB
app:miniFabDrawableTint android:tint Tints the icon drawable of the mini FAB(s)
app:miniFabBackgroundTint android:backgroundTint Tints the background colour of the mini FAB(s)


項目地址: https://github.com/yavski/fab-speed-dial

 本文由用戶 Annabelle 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!