【问题标题】:How to add a picture to android.support.v7.app.AlertDialog如何将图片添加到android.support.v7.app.AlertDialog
【发布时间】:2015-08-16 12:53:33
【问题描述】:

文档是这样说的:

如果要显示更复杂的视图,请查看 FrameLayout 称为“自定义”并将您的视图添加到其中:

FrameLayout fl = (FrameLayout) findViewById(android.R.id.custom);
 fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));

我的代码如下所示:

android.support.v7.app.AlertDialog alert = builder.create();

ImageView myView = new ImageView(mContext);
myView.setImageResource(R.drawable.image);

FrameLayout f1 = (FrameLayout) findViewById(android.R.id.custom);
f1.addView(myView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

alert.show();

f1 总是为 null,因此得到 NullPointerException。

如果我像下面这样更改代码,它没有帮助:

FrameLayout f1 = (FrameLayout) alert.findViewById(android.R.id.custom);

网站上有一个关于自定义布局的充气机的类似问题,但情况不同。

【问题讨论】:

  • 你能说出你到底想要什么吗,你有什么图片可以说明你想要的确切 UI
  • 我只想使用文档化的方式将视图添加到警报对话框布局中。

标签: android dialog alert


【解决方案1】:

您只需添加setView (View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight, int viewSpacingBottom)

     AlertDialog.Builder builder = alertDialogBuilder
            .setTitle("title")
            .setView(fl,24,24,24,24)
            .setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            {
                                dialog.cancel();
                            }
                        }
                    });

    AlertDialog alertDialog = builder .create();
    alertDialog.show();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多