AlertDialog.Builder使用AlertController.AlertParams来构造一个对话框。我检查了 AlertDialog.Builder#create() 调用 AlertController.AlertParams#apply() 创建 ListView 并分配适配器,如果项目已设置(AlertParams#createListView())。
我基于 createListView 源创建了自定义适配器并修改了 android 单元格布局:
public static class TypefaceDialog extends DialogFragment {
private static final CharSequence[] items = {
"A", "B", "C", "D", "E", "F", "G"
};
private static final boolean[] checked = {
true, false, false, true, true, false, false
};
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Typeface fontTypeface = Typeface.createFromAsset(getActivity().getAssets(), "Arial Bold.ttf");
ListAdapter adapter = new ArrayAdapter<CharSequence>(
getActivity(),
android.R.layout.select_dialog_multichoice,
android.R.id.text1,
items) {
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
CheckedTextView textView = (CheckedTextView)view.findViewById(android.R.id.text1);
textView.setChecked(checked[position]);
textView.setTypeface(fontTypeface);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckedTextView view = (CheckedTextView)v;
view.setChecked(!view.isChecked());
checked[position] = view.isChecked();
}
});
return view;
}
};
return new AlertDialog.Builder(getActivity())
.setAdapter(adapter, null)
.setPositiveButton("OK", null)
.create();
}
}