Android 警报对话框自定义面板和按钮面板高度为 0

Android alert dialog custom panel and button panel height is 0

我用来创建 dialog.Alert 对话框的函数 custtomPanel 和 buttonPanel 在使用大型数组创建时的高度均为 0。我该如何解决这个问题谢谢。

 private AlertDialog createDialog() {
    String[] alphabet = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
    //String[] alphabet = new String[]{"a", "b", "c", "d", "e", "f", "g", "h"};
    final boolean[] selectedAlphabets = new boolean[alphabet.length];
    int tempPosition = 0;
    for (String tempItem : alphabet) {
        selectedAlphabets[tempPosition] = false;
        tempPosition++;
    }

    View view = getLayoutInflater().inflate(R.layout.dialog_message_view, null);
    final TextView warning_message_tv = (TextView) view.findViewById(R.id.dialog_message);
    warning_message_tv.setText("-");


    final AlertDialog.Builder builder = new AlertDialog.Builder(this)
            .setTitle("Alert Dialog")
            .setCancelable(false)
            .setView(view)
            .setPositiveButton("Save",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    }
            ).setMultiChoiceItems(alphabet, selectedAlphabets, new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    selectedAlphabets[which] = isChecked;

                    int trueCount = 0;
                    for (int i = 0; i < selectedAlphabets.length; i++) {
                        if (selectedAlphabets[i]) {
                            trueCount++;
                        }
                    }
                    if (trueCount > 0) {
                        warning_message_tv.setText("* "+ trueCount + " items is selected");
                    }else {
                        warning_message_tv.setText("-");
                    }
                }
            }).setCancelable(true);

    return builder.create();
}

所以当我使用较短的数组时,按钮和自定义视图是可见的没有问题,但是当我使用较长的数组时,我的肯定按钮和自定义视图高度都是 0

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
   >

        <TextView
            android:id="@+id/dialog_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="5dp"
            android:textColor="#808080"
            android:minLines="2"
            android:gravity="center"
            android:text="* Lorem ipsum dolor sit amet, consectetur adipiscing elit."
            android:textSize="12sp" />


</LinearLayout>

试试这个方法:

    String[] alphabet = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
            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);
            layout.setPadding(50, 0, 50, 0);
    
            for (int i = 0; i < alphabet.length; i++) {
                CheckBox checkBox = new CheckBox(this);
                checkBox.setText(alphabet[i]);
                checkBox.setTextColor(Color.parseColor("#ff0000"));
                checkBox.setTextSize(24);
                checkBox.setPadding(10,5,10,5);
            final int finalI = i;
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Log.i("Onclick", finalI +" ->"+isChecked);
                }
            });
                layout.addView(checkBox);
            }
            scrollView.addView(layout);
            rootLayout.addView(scrollView);
            alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
    
                }
            });
            alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    
                }
            });
            alertDialog.setView(rootLayout);
            alertDialog.show();