Android setPrompt自定义字体

时间:2013-08-19 22:30:51

标签: java android

我有一个微调器设置,我将spinnerMode设置为对话框。我在我的程序中到处使用自定义字体,我想知道如何设置微调器的提示以使用我的自定义字体。我到处都看过如何做到这一点并且尚未找到它。

到目前为止,我是如何设置我的微调器的。

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);

1 个答案:

答案 0 :(得分:0)

您可能希望为Spinner设置样式:

一种方法:(那不行)

<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;
   }

}

在提示适配器中指定所需的字体:)

干杯,