我已经能够将自定义字体应用于Alert对话框的标题,如下所示: AlertDialog.Builder builder = new AlertDialog.Builder(this); TextView Mytitle = new TextView(this); Mytitle.setText("My Custom title"); Mytitle.setTextSize(20
AlertDialog.Builder builder = new AlertDialog.Builder(this); TextView Mytitle = new TextView(this); Mytitle.setText("My Custom title"); Mytitle.setTextSize(20); Mytitle.setPadding(5, 15, 5, 5); Mytitle.setGravity(Gravity.CENTER); Mytitle.setTypeface(Typeface.createFromAsset(this.getAssets(), "myfont.ttf")); builder.setCustomTitle(Mytitle);
警报对话框显示由下面一行填充的多选项目列表.
builder.setMultiChoiceItems(MyItems, MycheckedItems, MyDialogListener); //where MyItems is CharSequence[] Array, MycheckedItems => boolean[] array, //MyDialogListener => DialogInterface.OnMultiChoiceClickListener
我也希望将字体应用于这些多选项.我怎样才能做到这一点?可能吗?
AlertDialog.Builder使用 AlertController.AlertParams构建对话框.我检查了AlertDialog.Builder #create()调用AlertController.AlertParams #application()如果设置了项目,则创建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(); } }