【发布时间】:2020-08-06 01:52:40
【问题描述】:
我想做什么:
创建自定义警报对话框。按钮就像任何警报对话框一样,但上面是两个 TextEdit 输入框。我不想创建自定义对话框,而是自定义警报对话框
这是我正在尝试的#3: http://developer.android.com/guide/topics/ui/dialogs.html
上面写着:
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
文档说:
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
其中第一个参数是布局资源ID,第二个参数是根View的ID。
问题是我不知道布局根是什么?这是我将在 Activity 中启动的对话框。如果活动,我应该使用布局ID吗? layout_root 是不是脱胎换骨了?
也试过了:
View layout = inflater.inflate(R.layout.my_custom_layout,
(ViewGroup) findViewById(android.R.id.content).getRootView());
结果为空指针。
【问题讨论】:
标签: android