如何在正确的视图中从片段中显示 Snackbar?
How can a Snackbar be shown from a fragment in the correct view?
在我的片段中,我执行:
mActionHelper.showUndoBar(getView(), itemsList, lastPositionSelected);
showUndoBar() 方法只是创建一个具有以下形式的 Snackbar:
Snackbar.make(view, message, Snackbar.LENGTH_LONG);
但是,视图不知何故不正确,因为 Snackbar 不响应滑动以关闭手势,并且仅填满平板电脑拆分视图中的左下象限。大多数 Snackbar 示例都演示了从 Activity 调用 Snackbar,因此我认为我正在使用 Fragment 的事实是问题所在。如何获取并传递正确的视图以使 Snackbar 正确显示?
您的问题与您使用 Fragments 无关。
如果您的布局中没有 CoordinatorLayout
,您将不会启用滑动关闭功能,并且在平板电脑上,Snackbar 的显示将位于布局的最左下方。 Snackbar 有一个最大宽度,阻止它填满平板电脑的整个宽度,但是如果需要,您也可以使用 CoordinatorLayout
将 Snackbar 在平板电脑上居中。
来自Android Snackbar.make documentation:
Snackbar will try and find a parent view to hold Snackbar's view from
the value given to view. Snackbar will walk up the view tree trying to
find a suitable parent, which is defined as a CoordinatorLayout or the
window decor's content view, whichever comes first.
Having a CoordinatorLayout in your view hierarchy allows Snackbar to
enable certain features, such as swipe-to-dismiss and automatically
moving of widgets like FloatingActionButton.
在我的片段中,我执行:
mActionHelper.showUndoBar(getView(), itemsList, lastPositionSelected);
showUndoBar() 方法只是创建一个具有以下形式的 Snackbar:
Snackbar.make(view, message, Snackbar.LENGTH_LONG);
但是,视图不知何故不正确,因为 Snackbar 不响应滑动以关闭手势,并且仅填满平板电脑拆分视图中的左下象限。大多数 Snackbar 示例都演示了从 Activity 调用 Snackbar,因此我认为我正在使用 Fragment 的事实是问题所在。如何获取并传递正确的视图以使 Snackbar 正确显示?
您的问题与您使用 Fragments 无关。
如果您的布局中没有 CoordinatorLayout
,您将不会启用滑动关闭功能,并且在平板电脑上,Snackbar 的显示将位于布局的最左下方。 Snackbar 有一个最大宽度,阻止它填满平板电脑的整个宽度,但是如果需要,您也可以使用 CoordinatorLayout
将 Snackbar 在平板电脑上居中。
来自Android Snackbar.make documentation:
Snackbar will try and find a parent view to hold Snackbar's view from the value given to view. Snackbar will walk up the view tree trying to find a suitable parent, which is defined as a CoordinatorLayout or the window decor's content view, whichever comes first.
Having a CoordinatorLayout in your view hierarchy allows Snackbar to enable certain features, such as swipe-to-dismiss and automatically moving of widgets like FloatingActionButton.