【问题标题】:android - dynamically adding a button to a custom dialogboxandroid - 将按钮动态添加到自定义对话框
【发布时间】: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


    【解决方案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);

    【讨论】:

    • 它不起作用,布局返回为空,在我将 d.findViewById() 移到 d.getWindow() 之后它起作用了。你想解决它,添加一些解释,我会接受答案吗?
    • @thebeancounter 我编辑了我的答案并添加了解释,您现在可以接受答案
    • 太好了,谢谢,顺便说一句,当尝试添加两个按钮时,只出现一个。怎么样?
    • 您必须为您的按钮设置 ID。如果代码有效,请接受并投票回答,以便其他人可以信任和使用此代码。
    • 没问题,能否请您添加id的代码?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-23
      相关资源
      最近更新 更多