如何实现自定义AlertDialog视图

如何实现自定义AlertDialog视图

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中输入它就会产生编译错误。身体是在哪里定义或分配的?

这是我的密码。我试着用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();


喵喵时光机
浏览 534回答 3
3回答

慕村225694

你是对的,这是因为你没有手动充气。看起来,您正在尝试从活动的布局中“提取”body“id,但这是行不通的。你可能想要这样的东西:LayoutInflater inflater = getLayoutInflater();FrameLayout f1 = (FrameLayout)alert.findViewById(android.R.id.body);f1.addView(inflater.inflate(R.layout.dialog_view, f1, false));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android