【发布时间】:2018-09-22 14:21:54
【问题描述】:
我正在编写一个安卓应用程序, 我有一个活动,里面有一个按钮,它在点击侦听器上使用以下代码从自定义 XML 中打开一个对话框:
我想在该对话框中添加另一个未在其 XML 文件中设置的按钮。
所有组件都在 XML 中删除并且工作正常,对话框打开但我无法添加 b 按钮。
this.addCheer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog d = new Dialog(map.this);
LinearLayout layout = findViewById(R.id.dialog_layout_root);
LayoutInflater layoutInflater = d.getLayoutInflater();
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.cheer_dialog);// custom layout for the dialog
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
d.show();
d.getWindow().setAttributes(lp);
final EditText title = d.findViewById(R.id.cheerDialogText);
ImageButton addCheerOk = (ImageButton) d.findViewById(R.id.addCheerOk);
Button b = new Button(d.getContext());
b.setText("yo");
cheerDialogLayout.addView(b, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}
我尝试使用this 示例,但它对我不起作用。我在这里做错了什么? 谢谢!
【问题讨论】:
标签: java android android-layout android-fragments dynamic