gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:41:03 25 4
gpt4 key购买 nike

我用来创建 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();
}
所以当我使用较短的数组时,按钮和自定义 View 是可见的,但是当我使用较长的数组时,我的正按钮和自定义 View 高度都是 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>
dialog with short array {"a", "b", "c", "d", "e", "f", "g", "h"}
dialog with short array layout inspector height is 196
dialog array with all alphabet values
dialog with all alphabet values layout inspector height is 0

最佳答案

试试这个方法:

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

关于Android 警报对话框自定义面板和按钮面板高度为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64569564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com