Snackbar不会在滑动时关闭 [英] Snackbar is not dismissing on swipe

查看:108
本文介绍了Snackbar不会在滑动时关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的appcompat活动中有一个小吃店.它有一个确定按钮,可以关闭快餐栏.但是我无法在滑动(从左到右)时关闭小吃栏.

i have a snackbar in my appcompat activity. It has a button OK which dismiss the snackbar.It is working perfact. but i can't dismiss the snackbar on swipe(left to right).

以下是我的小吃店代码....

Following is my code for snackbar....

final Snackbar snackbar = Snackbar
                                .make(view, "Error Message", Snackbar.LENGTH_INDEFINITE);

                        snackbar.setAction("OK", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                            snackbar.dismiss();
                                    }
                                });

                        snackbar.show();

编辑1

我在活动的XML布局中将相对"布局作为父布局.

I have Relative layout as parent layout in my activity's XML layout.

推荐答案

Snackbar需要CoordinatorLayout作为其根布局或顶部布局,以执行其各种操作,例如滑动以关闭.您需要在布局层次结构中的某些位置.

Snackbar needs a CoordinatorLayout as its root layout or some where on top of it, to perform its various operations like swipe to dismiss. You need to have that some where in your layout hierarchy.

进一步,我们在Snackbar.make()方法中传递的视图用于在视图层次结构中的某些位置搜索CoordinatorLayout.该方法从该视图遍历到根视图,以找到一个CoordinatorLayout,可以在其上显示小吃栏并执行其动画和操作.

Further the view that we pass in the Snackbar.make() method is used to search a CoordinatorLayout some where in the view hierarchy. The method traverse from this view to the root view to find a CoordinatorLayout over which it can show the snackbar and perform its animations and operations.

因此,尝试将根布局替换为CoordinatorLayout,您的问题将得到解决.

So try replacing root layout to CoordinatorLayout and your problem will be solved.

这篇关于Snackbar不会在滑动时关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆