如何在BottomSheetDialogFragment中显示SnackBar?

26

我搜索了很多,但找不到解决方案我可以在对话框中显示材料设计Snackbar吗?,而Snackbar在片段类中不起作用也没有帮助。我传递了片段的rootView,也尝试从getActivity中传递视图,但它们都不起作用!

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.content_dialog_bottom_sheet, container, false);

Snackbar.make(MyActivity.myTextview, "Hello", Snackbar.LENGTH_INDEFINITE).show();

Snackbar.make(rootView, "Hello", Snackbar.LENGTH_INDEFINITE).show();

return rootView;

}

并且我的content_dialog_bottom_sheet:

 <RelativeLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:id="@+id/bottomSheetLayout"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="@color/background"
   app:behavior_hideable="true"
   app:behavior_peekHeight="180dp"
   app:layout_behavior="@string/bottom_sheet_behavior">

  //some views 

</RelativeLayout>

1
请添加一些代码。 - ImAtWar
@ImAtWar 我加了 - Hamed Ghadirian
1
请展示您的布局文件代码。 - ImAtWar
如果它仍然是实际的[https://dev59.com/nFsW5IYBdhLWcg3w4qiw#34477987]有所帮助。 - Alex
5个回答

50

解决方案非常简单。您需要:

  1. 使用 CoordinatorLayout 包装对话框布局(如果您想要在特定视图旁边显示 snackbar,请改为包装该视图)。
  2. 在显示 snackbar 时,使用 CoordinatorLayout 的 id 作为视图。

2
谢谢。 :)。这份工作花了2个小时来编写。 :) - Prashant Jajal

22
Snackbar.make(
            getDialog().getWindow().getDecorView(),
            "your-string",
            Snackbar.LENGTH_SHORT
    ).show();

将以下代码添加到您的onCreateView函数中:


1
如果有键盘可用,那么小吃店将被隐藏在其后面。 - AlexS

2

延迟后显示 snackbar:

   new Handler().postDelayed(new Runnable() {
  @Override
        public void run() {
            Snackbar.make(rootView, "Hello", Snackbar.LENGTH_INDEFINITE).show();

        }
    },200);

如果您想在 onCreateView 中显示 snackbar,请使用 postdelay 完成。否则,可以在按钮单击时直接展示它。 - Sonam Gupta
这是一个简单的点击监听器! - Hamed Ghadirian

2
如果您正在扩展您的Bottomsheet类到BottomSheetDialogFragment(),您可以使用对话框的decorView作为视图。
示例代码片段供参考:
Snackbar.make(
                            dialog?.window?.decorView,
                            "Press login button to continue",
                            Snackbar.LENGTH_LONG
                        )
                            .setAction("Login") { v: View? ->
                                val intent = Intent(context, DestinationActivityAfterLogin::class.java)
                                startActivity(intent)
                            }.show()

====

如果您正在使用BottomSheetDialog来显示底部菜单,那么可以使用rootView来弹出snackbar。
参考示例代码:
Snackbar.make(
                bsBinding.root.rootView, // bsBinding is view binding object
                "Press login button to continue",
                            Snackbar.LENGTH_LONG
                        )
                            .setAction("Login") { v: View? ->
                                val intent = Intent(context, DestinationActivityAfterLogin::class.java)
                                startActivity(intent)
                            }.show()

希望在显示与底部表单对话框一起的Snackbar时,这会很有用。
编码愉快。

1

使用@Wimukthi Rajapaksha的回答:

Snackbar.make(
        getDialog().getWindow().getDecorView(),
        "your-string",
        Snackbar.LENGTH_SHORT
).show();

您可以使用以下代码行将其显示在特定视图之上,请确保在设置锚点视图后再显示 Snackbar。

snackBar.anchorView = -您的视图-


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接