【问题标题】:How to show SnackBar in BottomSheetDialogFragment?如何在 BottomSheetDialogFragment 中显示 SnackBar?
【发布时间】:2017-11-23 20:59:39
【问题描述】:

我搜索了很多,但找不到任何解决方案 和 Snackbar is not working within fragment class 没有帮助。我传递了片段的 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>

【问题讨论】:

标签: android android-fragments bottom-sheet android-snackbar


【解决方案1】:
Snackbar.make(
            getDialog().getWindow().getDecorView(),
            "your-string",
            Snackbar.LENGTH_SHORT
    ).show();

将这种和平的代码添加到您的 onCreateView

【讨论】:

    【解决方案2】:

    解决方案相当简单。您需要:

    1. CoordinatorLayout 包裹您的对话框布局(如果您想在某个特定视图旁边显示小吃栏,请改为包裹它)
    2. 在显示小吃店时使用CoordinatorLayout id 作为视图。

    【讨论】:

    • 谢谢。 :)。这项工作花了2个小时到排水沟后。 :)
    【解决方案3】:

    延迟后显示小吃栏:

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

    【讨论】:

    • 如果你想在 oncreateview 中显示snackbar。用后延迟做到这一点。否则,单击按钮可以显示直接有效。
    • 它是一个简单的点击监听器!
    猜你喜欢
    • 2020-09-24
    • 2019-08-28
    • 2021-03-11
    • 1970-01-01
    • 2019-05-24
    • 2023-03-07
    • 2017-03-27
    • 2019-07-27
    • 2016-04-05
    相关资源
    最近更新 更多