【问题标题】:How to anchor simple button to snackbar如何将简单按钮锚定到小吃店
【发布时间】:2018-11-11 05:56:36
【问题描述】:

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

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

我怎么能用简单的按钮作为小吃栏和按钮不知道小吃栏的存在,反之亦然并且独立移动

谢谢

【问题讨论】:

    标签: java android android-button android-snackbar floating-action-button


    【解决方案1】:

    文档是这样说的

    在视图层次结构中包含 CoordinatorLayout 允许 Snackbar 启用某些功能,例如滑动关闭和自动移动小部件(如 FloatingActionButton)。

    所以只要你的按钮直接在 Coordinator Layout 下并且你像这样将按钮传递给 Snackbar

    Snackbar.make(yourButton, s, Snackbar.LENGTH_SHORT); 。向上移动小部件应该可以工作。

    【讨论】:

    • 它的简单按钮不是工厂
    • 它应该适用于任何类型的视图,因为它需要一个视图类型的对象作为参数。
    • Snackbar中的view用于查找布局的根视图,根视图则负责显示Snackbar,它不用于将View向上移动或下来。 CoordinateLayout 翻译 FAB 基于 Snackbar 运动的方式是通过其 Behavior 系统
    • @TamHuynh 你能回答吗
    • @blackHawk 你是什么意思?我确实在上面回答了,加上对Snakebar.makeview 参数的解释
    【解决方案2】:

    您必须将Behavior 附加到您想要移动的View 以及Snakebar 移动

    在这里我创建了一个自定义 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;
    }
    

    【讨论】:

    • 在您提供的代码中,按钮或视图应随小吃栏向上移动的状态?
    • 我用注释更新了代码,Gravity.BOTTOM 就是它发生的地方
    • CoordinateLayout 是您的根布局吗?你把它作为参数传递给Snakebar.make(coordinatorLayout, ...)
    • 是的,我做了,就像 fab.getRootView(); fab 是自定义按钮,我在您提供的代码中扩展了 Button 而不是 View
    • 好的,问题是您使用getRootView,它将返回最外层容器PhoneWindow.DecorView。这是CoordinatorLayout 的容器,因此使用当前逻辑找不到。准确传递CoordinatorLayout,或者按钮本身应该修复它
    猜你喜欢
    • 2023-01-05
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 2020-08-02
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 2020-05-18
    相关资源
    最近更新 更多