2013-08-19 100 views
0

我已經設置了微調器,並將spinnerMode設置爲對話框。我在程序中隨處使用自定義字體,我想知道如何設置微調器的提示以使用我的自定義字體。我到處尋找如何做到這一點,還沒有找到它。Android setPrompt自定義字體

以下是我迄今爲止如何設置我的微調器。

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       R.layout.my_spinner_style, items) { 

       public View getView(int position, View convertView, ViewGroup parent) { 
        View v = super.getView(position, convertView, parent); 
        Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Exo-Bold.otf"); 
        ((TextView) v).setTypeface(tf); 
        return v; 
       } 

       public View getDropDownView(int position, View convertView, ViewGroup parent) { 
        View v = super.getDropDownView(position, convertView, parent); 
        Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Exo-Bold.otf"); 
        ((TextView)v).setTypeface(tf); 
        //v.setBackgroundColor(Color.GREEN); 

        return v; 
       } 
     }; 

     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 


     gasChoice.setPrompt("Trip Type"); 
     gasChoice.setAdapter(adapter); 

回答

0

你可能想在風格類似微調:

一種方法:(這是行不通的)

<style name="DropDown" parent="android:style/Widget.Holo.Light.Spinner.DropDown.ActionBar"> 
    <item name="android:typeface">"myTypeFace"</item> <--- but there is no way to reference the custom typeface 
</style> 

第二種方法:

package com.example.spinner; 

import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.graphics.Typeface; 
import android.util.AttributeSet; 
import android.view.HapticFeedbackConstants; 
import android.view.View; 
import android.view.accessibility.AccessibilityEvent; 
import android.widget.ListAdapter; 
import android.widget.Spinner; 
import android.widget.TextView; 

public class SpinnerPlus extends Spinner implements DialogInterface.OnClickListener{ 


    private ListAdapter mPromptAdapter; 
    private AlertDialog mPromptDialog; 
    private OnLongClickListener mOnLongClickListener; 
    public SpinnerPlus(Context context, AttributeSet attrs, int defStyle, 
      int mode) { 
     super(context, attrs, defStyle, mode); 
     // TODO Auto-generated constructor stub 
    } 

    public SpinnerPlus(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     // TODO Auto-generated constructor stub 
    } 

    public SpinnerPlus(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 
    } 



    public SpinnerPlus(Context context, int mode) { 
     super(context, mode); 
     // TODO Auto-generated constructor stub 
    } 

    public SpinnerPlus(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 

    public void setPromptAdapter(ListAdapter adapter){ 
     mPromptAdapter = adapter; 
    } 
    @Override 
    public void setPrompt(CharSequence prompt) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); 
     TextView titleView = new TextView(getContext()); 
     setTypeFace(titleView); 
     builder.setCustomTitle(titleView); 
     builder.setSingleChoiceItems(mPromptAdapter, getSelectedItemPosition(), this); 
     mPromptDialog = builder.create(); 

    } 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     // TODO Auto-generated method stub 
     super.setSelection(which); 
     mPromptDialog.dismiss(); 
     mPromptDialog = null; 

    } 

    @Override 
    public boolean performClick() { 
     if(!performClick1()){ 
      mPromptDialog.show(); 
      return true; 
     } 
     return false; 
    } 
    @Override 
    public void setOnItemClickListener(OnItemClickListener l) { 
     super.setOnItemClickListener(l); 
    } 

    @Override 
    public void setOnLongClickListener(OnLongClickListener l) { 
     mOnLongClickListener = l; 
     super.setOnLongClickListener(l); 
    } 

    private void setTypeFace(TextView t){ 
     Typeface tf = Typeface.createFromAsset(t.getContext().getAssets(), "fonts/Exo-Bold.otf"); 
     t.setTypeface(tf); 
    } 
    private boolean performClick1(){ 
       sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED); 

       boolean handled = false; 
       if (mOnLongClickListener != null) { 
        handled = mOnLongClickListener.onLongClick(this); 
       } 
       if (!handled) { 
        handled = showContextMenu(); 
       } 
       if (handled) { 
        performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); 
       } 
       return handled; 
    } 

} 

指定需要的字體在提示適配器:)

乾杯,