使用自定义布局时AlertDialog按钮不可见

6

我希望在弹出窗口内使用自定义布局,所以我使用了AlertDialog。由于弹出窗口中子视图的数量是动态决定的,因此我使用了ScrollView。现在问题来了,当没有剩余空间并且滚动开始时,正面和负面按钮会变得不可见。 以下是代码:

AlertDialog.Builder dialog;
dialog = new AlertDialog.Builder(this);

dialog.setMessage("Please enter below parameters");

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT
);

LinearLayout rootLayout = new LinearLayout(this);
rootLayout.setOrientation(LinearLayout.VERTICAL);

ScrollView scrollView = new ScrollView(this);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(50, 0, 50, 0);


try {
                JSONArray data = new JSONArray(parameters);
                String toastDaata = "";
                for (int i = 0; i < data.length(); i++) {
                    JSONObject parameter = data.getJSONObject(i);
                    String sources = parameter.getString("sources");
                        String name = parameter.getString("name");
                        String mandatory = parameter.getString("mandatory");
                        toastDaata = toastDaata + "\n" + name + ":" + mandatory;

                        EditText editText$name = new EditText(this);
                        editText$name.setHint(name);
                        editText$name.setLayoutParams(params);
                        TextInputLayout textInputLayout = new TextInputLayout(this);
                        textInputLayout.setLayoutParams(params);
                        textInputLayout.addView(editText$name, params);
                        layout.addView(textInputLayout);

                                    }
                flowDescription.setText(toastDaata);
                Toast.makeText(this, toastDaata, Toast.LENGTH_SHORT).show();
            } catch (JSONException e) {
                e.printStackTrace();
            }

scrollView.addView(layout);
            rootLayout.addView(scrollView);


            dialog.setView(rootLayout);
            dialog.setNegativeButton("Cancel", null);
            dialog.setPositiveButton("Go", null);

            dialog.show();

enter image description here enter image description here

根据图片,当需要滚动时,它会隐藏按钮。

当我运行你的代码时,它完美地工作。 - Upendra Shah
@Pawan Dubey请检查我的答案。 - Vikas singh
3个回答

3

试试这个...对我来说很有效...

AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this); 
        LinearLayout rootLayout = new LinearLayout(this);
        rootLayout.setOrientation(LinearLayout.VERTICAL);
        ScrollView scrollView = new ScrollView(MainActivity.this);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        for (int i = 0; i < 20; i++) {
            EditText editText$name = new EditText(this);
            editText$name.setHint("Test");
            TextInputLayout textInputLayout = new TextInputLayout(this);
            textInputLayout.addView(editText$name);
            layout.addView(textInputLayout);
        }
        scrollView.addView(layout);
        rootLayout.addView(scrollView);
        alertDialog.setNegativeButton("Cancel", null);
        alertDialog.setPositiveButton("Go", null);
        alertDialog.setView(rootLayout);
        alertDialog.show();

与您期望的输出相同。

输入图像说明


1
感谢Vikas的努力。 在我添加alertDialog.setMessage("Any message");之前,这个程序是可以工作的。 - Pawan Dubey
@PawanDubey 如果你的信息很短,你可以使用 alertDialog.setTitle("任何消息");,因为它可以正常工作。 - Vikas singh
@PawanDubey 你是否曾经在不删除 .setMessage() 的情况下解决过这个问题? - btraas

0

使用 Dialog 而不是 Alert Dialog

Dialog dialog  = new Dialog(mContext);
                    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
                    dialog.setContentView(R.layout.activity_pop_up_payemnt);
                    DisplayMetrics dm=new DisplayMetrics();
                    mContext.getResources().getDisplayMetrics();
                    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
                    int width=dm.widthPixels;
                    int height=dm.heightPixels;
                    dialog.getWindow().setLayout((int)(width*.8),(int)(height*.7));
                    dialog.setCanceledOnTouchOutside(true);
                    WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
                    wmlp.gravity = Gravity.CENTER;
                    dialog.show();

0
尝试从底部为您的内部线性布局添加一些填充。
layout.setPadding(50, 0, 50, 20);

或者试试这个

scrollView.setFillViewport(true);

希望这能对你有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接