为了账号安全,请及时绑定邮箱和手机立即绑定

如何实现自定义AlertDialog视图

如何实现自定义AlertDialog视图

喵喵时光机 2019-07-09 12:45:31
如何实现自定义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();
查看完整描述

3 回答

?
慕村225694

TA贡献1880条经验 获得超4个赞

你是对的,这是因为你没有手动充气。看起来,您正在尝试从活动的布局中“提取”body“id,但这是行不通的。

你可能想要这样的东西:

LayoutInflater inflater = getLayoutInflater();FrameLayout f1 = (FrameLayout)alert.findViewById(android.R.id.body);f1.addView(inflater.inflate(R.layout.dialog_view, f1, false));


查看完整回答
反对 回复 2019-07-09
  • 3 回答
  • 0 关注
  • 531 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信