当你打开对话框窗口时我有它,5个选项下面有这个空白区域...在Eclipse中的DumpView层次结构内部,它说它是(2)FrameLayout [34,894] [686,1022]。
我的代码是:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.sync_frequency);
ListView listView = new ListView(getActivity());
String[] syncOptions = mSyncOptions;
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.select_dialog_singlechoice, android.R.id.text1, syncOptions);
builder.setSingleChoiceItems(modeAdapter, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
Log.d(TAG, "Position is " + selectedPosition);
}
});
builder.setView(listView);
builder.setNegativeButton(R.string.sync_cancel,null);
final Dialog dialog = builder.create();
dialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
dialog.show();
我试图摆脱那里的空白......我不知道如何摆脱它......
我甚至试过dialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
但它没有切断空间。我猜它因为它看起来像一个framlayout而不是空格。谢谢!
答案 0 :(得分:2)
您是否尝试将布局参数设置为listView(在充气到对话框之前)?
ListView listView = new ListView(getActivity());
listView.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
.
.
.
builder.setView(listView);
另一种选择是不在对话框中使用setView,而是使用setAdapter(),如下例所示:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.sync_frequency);
ListView listView = new ListView(getActivity());
String[] syncOptions = mSyncOptions;
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.select_dialog_singlechoice, android.R.id.text1, syncOptions);
builder.setAdapter(modeAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setNegativeButton(R.string.sync_cancel,null);
final Dialog dialog = builder.create();
dialog.show();
答案 1 :(得分:0)
这仅发生在API级别10+设备上。
要使用没有这些空间问题的旧对话框主题,请像这样初始化AlertDialog.Builder:
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Dialog));
答案 2 :(得分:0)
我已成功通过完全忽略其setAdapter()来删除AlertDialog列表末尾的额外空格。
我在调用builder.setView()之前创建一个ListView,并直接在其上设置Adapter和OnListItemClickListener。
我不知道为什么会这样,所以它可能在其他设备上破坏 - 希望不是灾难性的。