在AlertDialog上的Android文档中,它提供了以下用于在AlertDialog中设置自定义视图的说明和示例:
如果要显示更复杂的视图,请查找名为"body"的FrameLayout并将视图添加到其中:
FrameLayout fl = (FrameLayout) findViewById(R.id.body);
fl.add(myView, new LayoutParams(FILL_PARENT, WRAP_CONTENT));
Run Code Online (Sandbox Code Playgroud)
首先,非常明显的是这add()是一个错字,并且意味着addView().
我对使用R.id.body的第一行感到困惑.它似乎是AlertDialog的body元素......但是我不能在我的代码中输入它b/c它会产生编译错误.R.id.body在哪里被定义或分配或者其他什么?
这是我的代码.我试图setView(findViewById(R.layout.whatever)在构建器上使用但它没有用.我假设因为我没有手动充气吗?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title")
.setCancelable(false)
.setPositiveButton("Go", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
EditText textBox = (EditText) findViewById(R.id.textbox);
doStuff();
}
});
FrameLayout f1 = (FrameLayout)findViewById(R.id.body /*CURRENTLY an ERROR*/);
f1.addView(findViewById(R.layout.dialog_view));
AlertDialog alert = builder.create();
alert.show();
Run Code Online (Sandbox Code Playgroud)