在Android AlertDialog文档中,它提供了以下设置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的body元素...但我不能在我的代码中输入它,因为它会导致编译错误。R.id.body在哪里定义或分配了呢?
这是我的代码。我尝试在builder上使用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();
.setView(getLayoutInflater().inflate(R.layout.dialog_view, null))
。感谢下面的Sergio Viudes。 - 1''