深圳全飞鸿

标题: android.support.design.widget.FloatingActionButton [打印本页]

作者: zhgc    时间: 2019-5-22 21:43
标题: android.support.design.widget.FloatingActionButton
本帖最后由 zhgc 于 2019-5-22 21:46 编辑
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout     
  3.     xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:id="@+id/contentView"
  5.     android:orientation="vertical"     
  6.     android:layout_width="match_parent"
  7.     android:layout_height="match_parent">
  8.     <android.support.design.widget.FloatingActionButton
  9.         android:id="@+id/fab"
  10.         android:layout_width="wrap_content"
  11.         android:layout_height="wrap_content"
  12.         android:layout_alignParentBottom="true"
  13.         android:layout_alignParentRight="true"
  14.         android:onClick="onClick"
  15.         android:layout_marginRight="10dp"
  16.         android:layout_marginBottom="10dp"/>
  17. </RelativeLayout>
复制代码

  1. public void onClick(View v) {
  2.     switch (v.getId()) {
  3.         case R.id.fab:
  4.             Snackbar.make(findViewById(R.id.contentView), "Snackbar", Snackbar.LENGTH_SHORT).show();
  5.             break;
  6.     }
  7. }
复制代码

作者: zhgc    时间: 2019-5-22 21:57
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.design.widget.CoordinatorLayout
  3.     xmlns:android="http://schemas.android.com/apk/res/android"
  4.     xmlns:app="http://schemas.android.com/apk/res-auto"
  5.     android:id="@+id/contentView"
  6.     android:orientation="vertical"
  7.     android:layout_width="match_parent"
  8.     android:layout_height="match_parent">
  9.     <LinearLayout
  10.         android:id="@+id/anchorView"
  11.         android:layout_width="match_parent"
  12.         android:layout_height="match_parent"
  13.         android:orientation="vertical"/>
  14.     <android.support.design.widget.FloatingActionButton
  15.         android:id="@+id/fab"
  16.         android:layout_width="wrap_content"
  17.         android:layout_height="wrap_content"
  18.         app:layout_anchor="@id/anchorView"
  19.         app:layout_anchorGravity="bottom|right"
  20.         android:onClick="onClick"
  21.         android:layout_marginRight="10dp"
  22.         android:layout_marginBottom="10dp"/>
  23. </android.support.design.widget.CoordinatorLayout>
复制代码


CoordinatorLayout 提供了两个属性用来设置 FAB 的位置:

layout_anchor:设置 FAB 的锚点,我们熟悉的 PopupWindow 也有类似概念。
layout_anchorGravity:设置相对锚点的位置,如bottom|right表示 FAB 位于锚点的右下角。






欢迎光临 深圳全飞鸿 (http://www.nagomes.com/disc/) Powered by Discuz! X3.2