如何添加图片到android.support.v7.app.AlertDialog
How to add a picture to android.support.v7.app.AlertDialog
文档是这样说的:
If you want to display a more complex view, look up the FrameLayout
called "custom" and add your view to it:
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);
网站上有一个关于自定义布局充气机的类似问题,但这是不同的情况。
您只需添加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();
文档是这样说的:
If you want to display a more complex view, look up the FrameLayout called "custom" and add your view to it:
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);
网站上有一个关于自定义布局充气机的类似问题,但这是不同的情况。
您只需添加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();