代码之家  ›  专栏  ›  技术社区  ›  thebeancounter

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

  •  0
  • thebeancounter  · 技术社区  · 6 年前

    我正在写一个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 回复  |  直到 6 年前
        1
  •  1
  •   Nikunj Peerbits    6 年前

    只需尝试以下代码

    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);

        2
  •  0
  •   DeË£    5 年前

    这里的ll\u 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));