2013-02-26 96 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();