我已将自定义字体应用于Alert对话框的标题,如下所示:
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
我也希望将字体应用于这些多选项。我怎样才能做到这一点?有可能吗?
答案 0 :(得分:5)
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();
}
}
答案 1 :(得分:1)
尝试使用自定义项布局为多个选择创建自定义适配器。
在活动或应用程序中将字体设置为静态
public static Typeface mFont;
public void OnCreate(){
mFont =Typeface.createFromAsset(this.getAssets(), "myfont.ttf");
}
在自定义适配器中,使用静态字体从convertView设置textview的字体。
答案 2 :(得分:1)
而不是 AlertDialog.Builder 使用对话,如下所示。
在 R.layout.vv 内,按照您的意愿进行自定义。
Dialog dailog=new Dialog(this);
dailog.setContentView(R.layout.vv);
dailog.show();