如何解决Android警报对话框的自定义面板和按钮面板的高度为0
我用来创建对话框的函数。使用大数组创建时,警告对话框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","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,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",int which) {
}
});
alertDialog.setView(rootLayout);
alertDialog.show();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。