在有关 AlertDialog 的 Android 文档中,它提供了以下说明和示例,用于在 AlertDialog 中设置自定义视图:
如果您想显示更复杂的视图,请查找名为“body”的 FrameLayout 并将您的视图添加到其中:
FrameLayout fl = (FrameLayout) findViewById(R.id.body);
fl.add(myView, new LayoutParams(FILL_PARENT, WRAP_CONTENT));
首先,很明显这add()
是一个错字,应该是addView()
.
我对使用 R.id.body 的第一行感到困惑。它似乎是 AlertDialog 的主体元素......但我不能只在我的代码 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();