AlertDialog完美地显示内容和按钮,直到内容增长到需要滚动才能显示所有内容为止。此时,即使列表完整显示,按钮也会消失。
我看到在过去的几年里,有人问过这个问题(这个问题基本上与我的问题(DialogFragment buttons pushed off screen API 24 and above相同),但是答案似乎都是命中注定的,甚至偶然的:设置layout_weight,从setMessage()切换设置为setTitle(),最后从AlertDialog切换到Dialog。
这些都不对我有用,并且避免使用AlertDialog并不是一个吸引人的选择,因为我正在使用setMultiChoiceItems()方法,该方法似乎(对于我来说是不可思议的,也许是因为我的知识非常有限)只能用于AlertDialog。
在绝望和沮丧中,我直接吸引了stackoverflow的集体大脑。
这是我的DialogFragment的简化版本:
public class SurfaceDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getactivity());
LayoutInflater inflater = requireactivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.surface_dialog,null))
.setTitle(mMessage)
.setMultiChoiceItems(mSurfaceList,mCheckedSurfaces,new DialogInterface.OnmultiChoiceclicklistener() {
@Override
public void onClick(DialogInterface dialog,int which,boolean isChecked) {
if (isChecked) {
mCheckedSurfacesPositions.add(which);
} else {
mCheckedSurfacesPositions.remove(mCheckedSurfacesPositions.indexOf(which));
}
}
});
// builder.setCancelable(false);
builder.setPositiveButton("Done",new DialogInterface.OnClicklistener() {
@Override
public void onClick(DialogInterface dialog,int which) {
ProcessDialogSelections(mCheckedSurfacesPositions);
dismiss();
}
}).setNegativeButton("Cancel",int which) {
listener.onSurfaceDialogSelectedSent(mUnitSurfaces);
dismiss();
}
});
return builder.create();
}
那不起作用,然后我编码了自己的按钮并显示了它们:
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getactivity());
LayoutInflater inflater = requireactivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.surface_dialog,null);
builder.setView(dialogView)
.setTitle(mMessage)
.setMultiChoiceItems(mSurfaceList,boolean isChecked) {
if (isChecked) {
mCheckedSurfacesPositions.add(which);
} else {
mCheckedSurfacesPositions.remove(mCheckedSurfacesPositions.indexOf(which));
}
}
});
Button positiveButton = dialogView.findViewById(R.id.positive_button);
Button negativeButton = dialogView.findViewById(R.id.negative_button);
positiveButton.setOnClicklistener(new View.OnClicklistener() {
@Override
public void onClick(View v) {
ProcessDialogSelections(mCheckedSurfacesPositions);
dismiss();
}
});
negativeButton.setOnClicklistener(new View.OnClicklistener() {
@Override
public void onClick(View v) {
listener.onSurfaceDialogSelectedSent(mUnitSurfaces);
dismiss();
}
});
return builder.create();
}
这是我修改后的XML文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
android:orientation="vertical">
</ScrollView>
<Button
android:id="@+id/positive_button"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_width="wrap_content"
android:text="Done"
app:layout_constraintBaseline_toBaselineOf="@+id/negative_button"
app:layout_constraintEnd_toStartOf="@+id/negative_button"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/negative_button"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_width="wrap_content"
android:text="Cancel"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/positive_button" />
</androidx.constraintlayout.widget.ConstraintLayout>
在没有任何效果的情况下,我遵循了Vikas Singh(AlertDialog Buttons not visible when using Custom layout)的建议。这就是新代码的样子(基本上是用Java而不是XML创建布局。
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getactivity());
// New code from Vikas Singh
LinearLayout rootLayout = new LinearLayout(getactivity());
rootLayout.setOrientation(LinearLayout.VERTICAL);
ScrollView scrollView = new ScrollView(getactivity());
LinearLayout layout = new LinearLayout(getactivity());
layout.setOrientation(LinearLayout.VERTICAL);
scrollView.addView(layout);
rootLayout.addView(scrollView);
builder.setNegativeButton("Cancel",null);
builder.setPositiveButton("Go",null);
builder.setView(rootLayout);
builder//.setView(dialogView)
//.setTitle(mMessage)
.setMultiChoiceItems(mSurfaceList,boolean isChecked) {
if (isChecked) {
mCheckedSurfacesPositions.add(which);
} else {
mCheckedSurfacesPositions.remove(mCheckedSurfacesPositions.indexOf(which));
}
}
});
return builder.create();
}
当我必须滚动时,仍然没有按钮。 为什么不呢,我该怎么办?