【问题标题】:Can I display material design Snackbar in dialog?我可以在对话框中显示材料设计 Snackbar 吗?
【发布时间】:2015-12-03 22:56:23
【问题描述】:

我正在开发一个安卓应用程序。在那我想在对话框中显示材料设计 Snackbar。可能吗?如果是那怎么办?

请帮帮我。

谢谢。

【问题讨论】:

  • 从技术上讲,应该是有问题的。真正的问题是为什么需要它
  • 我认为你不能在对话框中使用 Snackbar,因为它总是显示在屏幕底部。此外,我必须说,你不应该在对话框中使用 Snackbar,因为它不是正确的使用方式。
  • 别忘了接受其中一个答案! :)

标签: android material-design android-snackbar


【解决方案1】:

绝对有可能,您只需将对话框的视图传递给SnackBar

示例

    AlertDialog.Builder mAlertDialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    // inflate the custom dialog view
    final View mDialogView = inflater.inflate(R.layout.dialog_layout, null);
    // set the View for the AlertDialog
    mAlertDialogBuilder.setView(mDialogView);

    Button btn = (Button) mDialogView.findViewById(R.id.dialog_btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Pass the mDialogView to the SnackBar
            Snackbar
                    .make(mDialogView, "SnackBar in Dialog", Snackbar.LENGTH_LONG)
                    .show();
        }
    });
    AlertDialog alertDialog = mAlertDialogBuilder.create();
    alertDialog.show();

结果

注意: 无需使用CoordinatorLayout 作为根。在我的示例中,我只是使用 LinearLayout 作为根。

【讨论】:

  • 不使用 CoordinatorLayout 时可能无法滑动 SnackBar。
【解决方案2】:

是的,你可以。

要在您的Dialog 中显示Snackbar,请为其创建自定义View。你可以在这里阅读更多信息:Dialogs/Creating a Custom Layout

然后显示Snackbar 调用Snackbar.make((dialogView, "text", duration)) 其中dialogView 是您的自定义视图。

【讨论】:

    【解决方案3】:

    如果您使用的是Dialog,那么:

    dialog_share = new Dialog(MainScreen.this, R.style.DialogTheme);
    dialog_share.requestWindowFeature(Window.FEATURE_NO_TITLE);
    LayoutInflater inflater = this.getLayoutInflater();
    mDialogView = inflater.inflate(R.layout.dialog_share, null);
    dialog_share.setContentView(mDialogView);
    dialog_share.getWindow().setBackgroundDrawableResource(R.color.translucent_black);
    dialog_share.show();
    
    public void ShowSnackBarNoInternetOverDialog() {
            Snackbar snackbar = Snackbar.make(mDialogView, getString(R.string.checkinternet), Snackbar.LENGTH_LONG);
            snackbar.setActionTextColor(Color.CYAN);
            snackbar.setAction("OK", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Toast.makeText(MainScreen.this, "snackbar OK clicked", Toast.LENGTH_LONG).show();
                }
            });
            snackbar.show();
        }
    

    【讨论】:

      【解决方案4】:

      在 Snackbar.make() 中使用 getDialog().getWindow().getDecorView()

      Snackbar
            .make(getDialog().getWindow().getDecorView(), "SnackBar in Dialog", Snackbar.LENGTH_LONG)
            .show();
      

      【讨论】:

        猜你喜欢
        • 2015-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-26
        相关资源
        最近更新 更多