首發于我的博客,轉載請注明作者和原文鏈接。
FloatActionButton
一個變相的ImageView。
屬性介紹
app:fabSize 定義FAB的大小,可選:[ auto | mini | normal ]
app:elevation 普通狀態下的陰影深度
app:pressedTranslationZ 按下狀態的陰影深度
app:backgroundTint 默認的背景顏色
app:rippleColor 按下時的背景顏色
app:border border的寬度
app:layout_anchor 定位于其他控件,表現形式就是中點跟其他控件的邊界相交
app:layout_anchorGravity 在其他控件上的相對位置
app:useCompatPadding 設置內邊距
XML布局
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".collapsingtoolbarlayout.CollapsingToolbarLayoutActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:id="@+id/app_bar_layout"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="240dp"
android:id="@+id/collapsing_toolbar_layout"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:navigationIcon="?attr/homeAsUpIndicator"
app:layout_collapseMode="pin">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
app:useCompatPadding="true"
app:layout_anchor="@id/app_bar_layout"
app:layout_anchorGravity="bottom|end"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_margin="@dimen/spacing_16"
android:layout_gravity="bottom|end"/>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/ipsum"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
與AppBarLayout
和CollapsingToolbarLayout
組合使用,將自身錨定在上面。Toolbar
完全折疊時,FAB也會消失。
Snackbar
類似于Toast的提示控件。
Java代碼
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_floatactionbutton);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}