0

我已经设置了一个微调器,并且我将 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);
4

1 回答 1

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

}

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

干杯,

于 2013-08-20T02:59:17.930 回答