您可能希望将 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;
}
}
在提示适配器中指定所需的字体:)
干杯,