Android Design Support Library之FAB(Floating Action Button)

FAB是Android Design Support Library庫中帶有陰影的圓形button,他的使用非常簡單。一般使用FAB時需要確定其位置,所以可以在外部套用Framelayout。如:

<FrameLayout
        android:id="@+id/rootLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fabBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:src="@drawable/ic_plus"
            app:fabSize="normal" />
    </FrameLayout>

  不過我們一般都是和CoordinatorLayout結合使用。CoordinatorLayout是加強型FrameLayout,它實現了和FAB的聯動。
  
  佈局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/cl_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|bottom"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="8dp"
        android:src="@mipmap/ic_plus"/>
</android.support.design.widget.CoordinatorLayout>

然後在代碼中給FAB設置監聽即可,當然也可以結合Snackbar使用:

 fab_button= (FloatingActionButton) findViewById(R.id.fab_button);
        fab_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(cl_root,R.string.snake_show_text,Snackbar.LENGTH_SHORT)
                        .setAction(R.string.snake_action_text, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                            // do something
                                finish();
                            }
                        }).show();
            }
        });

  這樣就可以完成懸浮按鈕的操作了。

這裏寫圖片描述

總體來說FAB是Android Design Support Library中最簡單的控件了,但是他的作用卻還是很大的。我們可以通過自定義他的佈局來實現我們想要的懸浮按鈕。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章