Android FloatingActionButton控件

1. FloatingActionButton类

FloatingActionButton是5.0版本出现的控件,显示一个圆形悬浮按钮。需要添加Design依赖库,并且使用Theme.AppCompat主题。

2. FloatingActionButton配置

FloatingActionButton的配置

  • android:src,显示的图标,最好是24dp的
  • app:backgroundTint,正常的背景颜色
  • app:rippleColor,按下时的背景颜色
  • app:elevation,正常的阴影大小(默认6dp)
  • app:pressedTranslationZ,按下时的阴影大小(默认12dp)
  • app:borderWidth,边框宽度
  • app:layout_anchor,设置FAB的锚点,即以哪个控件为参照设置位置
  • app:layout_anchorGravity,FAB相对于锚点的位置
  • app:fabSizenormalmini(对应56dp和40dp)

配置文件


效果如下
Android FloatingActionButton控件_第1张图片

3. Snackbar交互

FloatingActionButton如果没有与Snackbar关联,容易被Snackbar覆盖。
Android FloatingActionButton控件_第2张图片
设置FloatingActionButtonCoordinatorLayout内部,同时将这个CoordinatorLayout作为View参数传给Snackbarmake()方法。
Android FloatingActionButton控件_第3张图片
查看了一下Snackbar的源码,如果父视图是CoordinatorLayout,创建Snackbar的时候,会设置LayoutParamsinsetEdge属性。

final void showView() {
    if (mView.getParent() == null) {
        final ViewGroup.LayoutParams lp = mView.getLayoutParams();
        if (lp instanceof CoordinatorLayout.LayoutParams) {
            // If our LayoutParams are from a CoordinatorLayout, we'll setup our Behavior
            final CoordinatorLayout.LayoutParams clp = (CoordinatorLayout.LayoutParams) lp;

            final Behavior behavior = new Behavior();
            behavior.setStartAlphaSwipeDistance(0.1f);
            behavior.setEndAlphaSwipeDistance(0.6f);
            behavior.setSwipeDirection(SwipeDismissBehavior.SWIPE_DIRECTION_START_TO_END);
            behavior.setListener(new SwipeDismissBehavior.OnDismissListener() {
                @Override
                public void onDismiss(View view) {
                    view.setVisibility(View.GONE);
                    dispatchDismiss(BaseCallback.DISMISS_EVENT_SWIPE);
                }

                @Override
                public void onDragStateChanged(int state) {
                    switch (state) {
                        case SwipeDismissBehavior.STATE_DRAGGING:
                        case SwipeDismissBehavior.STATE_SETTLING:
                            // If the view is being dragged or settling, pause the timeout
                            SnackbarManager.getInstance().pauseTimeout(mManagerCallback);
                            break;
                        case SwipeDismissBehavior.STATE_IDLE:
                            // If the view has been released and is idle, restore the timeout
                            SnackbarManager.getInstance()
                                    .restoreTimeoutIfPaused(mManagerCallback);
                            break;
                    }
                }
            });
            clp.setBehavior(behavior);
            // Also set the inset edge so that views can dodge the bar correctly
            clp.insetEdge = Gravity.BOTTOM;
        }

        mTargetParent.addView(mView);
    }

    ... ...
}

相关文章
Android Snackbar控件
Android FloatingActionButton控件
Android Toolbar控件
Android AppBarLayout控件
Android CollapsingToolbarLayout控件
Android CardView控件
Android SlidingPaneLayout和DrawerLayout控件
Android NavigationView控件
Android TabLayout控件

你可能感兴趣的:(Android,Material,Design)