android spinner更改字体字体

我将在我的微调器中将文本字体更改为arialbold我将如何这样做。 以下是我的代码: –

ArrayAdapter genderAdap = ArrayAdapter.createFromResource(getActivity(), R.array.gender,R.layout.spinner_item); genderAdap.setDropDownViewResource(R.layout.spinner_item); ddlGender.setAdapter(genderAdap); 

分类:

 public class testActivity extends Activity { private static final String[] COUNTRIES = new String[] { "Belgium", "France", "Italy", "Germany", "Spain" }; private Spinner mySpinner; private Typeface myFont; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.newlay); mySpinner = (Spinner) findViewById(R.id.spinner1); myFont = Typeface.createFromAsset(getAssets(), "gujarti.ttf"); MyArrayAdapter ma = new MyArrayAdapter(this); mySpinner.setAdapter(ma); } private class MyArrayAdapter extends BaseAdapter { private LayoutInflater mInflater; public MyArrayAdapter(testActivity con) { // TODO Auto-generated constructor stub mInflater = LayoutInflater.from(con); } @Override public int getCount() { // TODO Auto-generated method stub return COUNTRIES.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub final ListContent holder; View v = convertView; if (v == null) { v = mInflater.inflate(R.layout.my_spinner_style, null); holder = new ListContent(); holder.name = (TextView) v.findViewById(R.id.textView1); v.setTag(holder); } else { holder = (ListContent) v.getTag(); } holder.name.setTypeface(myFont); holder.name.setText("" + COUNTRIES[position]); return v; } } static class ListContent { TextView name; } } 

布局:

    

my_spinner_style.xml

    

TTF文件

您可以通过以下方式设置适配器,如下所示:

 mMsgAdap =new ArrayAdapter(getApplicationContext(), R.layout.spinner_item_list, mCategories) { public View getView(int position, View convertView, ViewGroup parent) { View v = super.getView(position, convertView, parent); ((TextView) v).setTypeface(StaticUtils.sTypeFace(getApplicationContext()));//Typeface for normal view return v; } public View getDropDownView(int position, View convertView, ViewGroup parent) { View v = super.getDropDownView(position, convertView, parent); ((TextView) v).setTypeface(StaticUtils.sTypeFace(getApplicationContext()));//Typeface for dropdown view ((TextView) v).setBackgroundColor(Color.parseColor("#BBfef3da")); return v; } }; mMsgAdap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mMsgSpnr.setAdapter(mMsgAdap); mMsgSpnr.setSelection(0); 

我在我创建的StaticUtils类中静态采用的字体方法是

 public static Typeface sTypeFace(Context mCnxt) { Typeface mtypeface = Typeface.createFromAsset(mCnxt.getAssets(), "fonts/forte-mt-1361539051.ttf"); return mtypeface; } 

要更改微调器项的字体,必须使用自定义微调器

尝试在微调器中为所有TextView设置自定义字体:

 Typeface typeface; font_name_Adapter= new ArrayAdapter (this,android.R.layout.simple_spinner_item,ARRLIST_FONTS) { public View getView(int position, View convertView,ViewGroup parent) { View v = super.getView(position, convertView, parent); ((TextView) v).setTextSize(12); ((TextView) v).setTextColor(Color.WHITE); if(position<=ARRLIST_FONTS.size()){ typeface = Typeface.createFromAsset(Your_Current_Activity.this.getAssets(), "fonts/yourfontname.ttf"); ((TextView) v).setTypeface(typeface); } return v; } 

您可以通过自己的自定义SpinnerAdapter,在getView()和getDropDownView()中应用字体

 public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = getLayoutInflater(); View row = inflater.inflate(yourRowlayout, parent, false); TextView make = (TextView) row.findViewById(R.id.Make); Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(), "fonts/gilsanslight.otf"); v.setTypeface(myTypeFace); v.setText(itemList.get(position)); return row; } public View getDropDownView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = getLayoutInflater(); View row = inflater.inflate(yourRowlayout, parent, false); TextView make = (TextView) row.findViewById(R.id.Make); Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(), "fonts/gilsanslight.otf"); v.setTypeface(myTypeFace); v.setText(itemList.get(position)); return row; } 
 // try this custom adapter for spinner class MyCustomAdapter extends ArrayAdapter { Context context; ArrayList list; private int defaultPosition; public int getDefaultPosition() { return defaultPosition; } public MyCustomAdapter(Context context, ArrayList objects) { super(context, 0, objects); this.context = context; list = objects; } public void setDefaultPostion(int position) { this.defaultPosition = position; } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { return getCustomView(position, convertView, parent); } @Override public View getView(int position, View convertView, ViewGroup parent) { return getCustom(position, convertView, parent); } public View getCustom(int position, View convertView, ViewGroup parent) { View row = LayoutInflater.from(context).inflate( android.R.layout.simple_spinner_item, parent, false); TextView label = (TextView) row.findViewById(android.R.id.text1); Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/yourfontsname"); label.setTypeface(tf); label.setText(list.get(position)); return row; } public View getCustomView(int position, View convertView, ViewGroup parent) { View row = LayoutInflater.from(context).inflate( android.R.layout.simple_spinner_item, parent, false); TextView label = (TextView) row.findViewById(android.R.id.text1); Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/yourfontname"); label.setTypeface(tf); label.setText(list.get(position)); return row; } }