android - 动态添加按钮到自定义对话框

时间:2018-04-12 12:21:11

标签: java android android-layout android-fragments dynamic

我正在写一个Android应用程序, 我有一个活动,里面有一个按钮,它在点击监听器上使用以下代码从自定义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示例,但它对我不起作用。我在这做错了什么? 谢谢!

2 个答案:

答案 0 :(得分:1)

试试以下代码

this.addCheer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Dialog d = new Dialog(map.this);
                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);

                LinearLayout layout = d.findViewById(R.id.dialog_layout_root);
                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");

                layout.addView(b, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}

您仅使用findViewById(R.id.dialog_layout_root);代替d.findViewById(R.id.dialog_layout_root);

答案 1 :(得分:0)

这里ll_button是布局的id,它包含你拥有的两个xml按钮。

LinearLayout ll_button = d.findViewById(R.id.ll_button); LinearLayout.LayoutParams layoutParams = new inearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); ll_button.addView(b, layoutParams);

删除你的最后一行。

cheerDialogLayout.addView(b, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));