使用自定义布局时警报对话框按钮不可见

Paw*_*bey 5 android

我想要在弹出窗口中自定义布局,所以我使用了警报对话框。由于弹出窗口中子视图的数量是动态决定的,所以我使用了 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();
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明 在此处输入图片说明

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

Vik*_*ngh 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();
Run Code Online (Sandbox Code Playgroud)

与您相同的输出除外..

在此输入图像描述