简体   繁体   English

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

[英]AlertDialog Buttons not visible when using Custom layout

I wanted a custom layout inside the pop up, so I used Alert Dialog. 我想要弹出一个自定义布局,所以我使用了Alert Dialog。 As the number of subviews in the popup is dynamically decided so I used ScrollView. 由于弹出窗口中的子视图的数量是动态决定的,所以我使用了ScrollView。 Now the twist comes here, when there is no space left and scroll gets in action, the positive and negative buttons gets invisible. 现在扭曲来到这里,当没有剩余空间并且滚动开始动作时,正面和负面按钮变得不可见。 Below is the code: 以下是代码:

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

在此输入图像描述 在此输入图像描述

As per image when scroll needed it hides the buttons. 根据需要滚动时的图像隐藏按钮。

Try this .. It's working for me... 试试这个..它对我有用......

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

Same Output As you excepted.. 相同的输出你除外..

在此输入图像描述

use Dialog instead of Alert Dialog 使用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();

Try to give some padding for your inner linear layout from bottom. 尝试从底部为内部线性布局提供一些填充。

layout.setPadding(50, 0, 50, 20);

Or Try this 或试试这个

scrollView.setFillViewport(true);

Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM