2013-02-26 98 views
2

我已经能够自定义字体应用于警报对话框的标题如下:设置自定义字体或符号(字体)为AlertDialog的MultiSelectItems

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 

我想要的字体应用于这些多选项目也。我怎样才能做到这一点?可能吗?

回答

4

AlertDialog.Builder使用AlertController.AlertParams来构建对话框。如果设置了项目(AlertParams#createListView()),我检查了AlertDialog.Builder#create()调用AlertController.AlertParams#apply(),它将创建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(); 
     } 

    } 
+0

非常感谢。我在很长一段时间后尝试了这一点。它就像一个魅力。 – SKK 2013-04-17 11:39:46

+0

是否可以设置正面,中性和负面按钮的字体? – SKK 2013-05-20 07:32:52

+0

你可以把这个类的例子用法吗? – busuu 2017-07-09 21:44:21

1

尝试使用自定义项目布局创建用于多选的自定义适配器。

设置的字体为静态您的活动中或应用

public static Typeface mFont; 
public void OnCreate(){ 
mFont =Typeface.createFromAsset(this.getAssets(), "myfont.ttf"); 
} 

在您的自定义适配器,使用静态字体设置的TextView的字样从convertView。

+0

“开创了多个自定义适配器具有自定义项目布局选择。”你能给我多一点这方面的意见吗? – SKK 2013-02-26 09:40:38

+0

这是一个有用的博客http://adanware.blogspot.com/2012/04/android-multiple-selection-listview.html – CarlCarrots 2013-03-01 08:56:14

1

而不是AlertDialog.Builder使用对话框像如下。

里面R.layout.vv自定义就像你想要的。

Dialog dailog=new Dialog(this); 
dailog.setContentView(R.layout.vv); 
dailog.show();