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

时间:2018-01-17 10:03:27

标签: android

我想要弹出窗口中的自定义布局,所以我使用了Alert Dialog。由于弹出窗口中的子视图的数量是动态决定的,所以我使用了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

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

3 个答案:

答案 0 :(得分:2)

试试这个..它为我工作......

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

同样的输出你除外..

enter image description here

答案 1 :(得分: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();

答案 2 :(得分:0)

尝试从底部为内部线性布局提供一些填充。

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

或试试这个

scrollView.setFillViewport(true);

希望这会对你有所帮助。