Android 为AlertDialog的多选项设置自定义字体(字体)
我已经能够将自定义字体应用于警报对话框的标题,如下所示:Android 为AlertDialog的多选项设置自定义字体(字体),android,android-alertdialog,custom-font,Android,Android Alertdialog,Custom Font,我已经能够将自定义字体应用于警报对话框的标题,如下所示: 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(
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
我还想对这些多选项应用字体。我该怎么做?有可能吗?尝试使用自定义项目布局为multiple select创建自定义适配器 在活动或应用程序中将字体设置为静态
public static Typeface mFont;
public void OnCreate(){
mFont =Typeface.createFromAsset(this.getAssets(), "myfont.ttf");
}
在自定义适配器中,使用静态字体从convertView设置textview的字体。而不是AlertDialog。生成器使用如下对话框
在R.layout.vv内部,根据需要进行定制
Dialog dailog=new Dialog(this);
dailog.setContentView(R.layout.vv);
dailog.show();
用于构造对话框。我检查了AlertDialog.Buildercreate调用AlertController.AlertParamsapply,如果设置了项,它将创建ListView并分配适配器
我基于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();
}
}
使用自定义项布局为多个选择创建自定义适配器。你能给我更多的意见吗?这是一个有用的博客,非常感谢。我试了很久。它的作用就像一个符咒。有没有可能设置正面、中性和负面按钮的字体?你能举例说明这个类的用法吗?