有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将简单按钮锚定到snackbar

我有一个按钮,而不是浮动动作按钮,我想用小吃店固定它,因为它用浮动动作栏固定,就像浮动动作按钮在显示小吃店时向上移动一样,用浮动动作栏我们这样做:

Snackbar.make(fab, s, Snackbar.LENGTH_SHORT);

我如何使用简单的按钮,比如snackbar和button,它们不知道snackbar的存在,反之亦然,并且可以独立移动

谢谢


共 (1) 个答案

  1. # 1 楼答案

    您必须将Behavior附加到要随Snakebar移动的View

    在这里,我创建了一个自定义View,并将行为附加到它

    @CoordinatorLayout.DefaultBehavior(CustomView.Behavior.class)
    public class CustomView extends View {
        public CustomView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
    
        public static class Behavior extends CoordinatorLayout.Behavior<CustomView> {
    
            public Behavior() {
                super();
            }
    
            public Behavior(Context context, AttributeSet attrs) {
                super(context, attrs);
            }
    
            @Override
            public void onAttachedToLayoutParams(@NonNull CoordinatorLayout.LayoutParams lp) {
                if (lp.dodgeInsetEdges == Gravity.NO_GRAVITY) {
                    // This setting makes it works (learn from FloatingActionButton.Behavior)
                    lp.dodgeInsetEdges = Gravity.BOTTOM;
                }
            }
    
            @Override
            public boolean onDependentViewChanged(CoordinatorLayout parent, CustomView child,
                                                  View dependency) {
                return false;
            }
        }
    }
    

    在你的布局中使用这个CustomView,它应该可以工作

    FloatingActionButton做同样的事情,它有一个内部的Behavior,它通过阴影填充、基于AppBarLayout运动的显示/隐藏来做更多的事情。在CoordinatorLayout中的每一个运动都基于它的Behavior机制

    此处参考:http://saulmm.github.io/mastering-coordinator

    p/S:关于Snakebar.make用法,将CoordinatorLayout传递到view参数,不传递任何其他组件,这将节省系统性能。如果查看Snakebar代码,它有一个循环,使用view参数查找根视图。根视图就是它想要的:

    private static ViewGroup findSuitableParent(View view) {
        ViewGroup fallback = null;
        do {
            if (view instanceof CoordinatorLayout) {
                // We've found a CoordinatorLayout, use it
                return (ViewGroup) view;
            } else if (view instanceof FrameLayout) {
                if (view.getId() == android.R.id.content) {
                    // If we've hit the decor content view, then we didn't find a CoL in the
                    // hierarchy, so use it.
                    return (ViewGroup) view;
                } else {
                    // It's not the content view but we'll use it as our fallback
                    fallback = (ViewGroup) view;
                }
            }
    
            if (view != null) {
                // Else, we will loop and crawl up the view hierarchy and try to find a parent
                final ViewParent parent = view.getParent();
                view = parent instanceof View ? (View) parent : null;
            }
        } while (view != null);
    
        // If we reach here then we didn't find a CoL or a suitable content view so we'll fallback
        return fallback;
    }