如何以编程方式将字体自定义字体设置为微调文本?

我的资产文件夹中有一个ttf字体文件。 我知道如何将它用于文本浏览:

Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/HelveticaNeueLTCom-Lt.ttf"); textview1.setTypeface(externalFont); 

我已经定义寻找我自己的微缩文本在它自己的XML文件(如在Android中使用):

 <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+android:id/text1" style="?android:attr/spinnerItemStyle" android:singleLine="true" android:textColor="#ffffff" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:ellipsize="marquee" /> 

我只是不能从代码引用这个textview,我总是得到空指针exception。 例如,我试过:

 TextView spinner_text=(TextView)findViewById(R.id.text1); spinner_text.setTypeface(externalFont); 

是否有可能select我的外部字体,即使我自己的xml中定义的微调文本?

谢谢。

编辑答案:

这工作:

 String [] items = new String[2]; items[0]="Something1"; items[1]="Something2"; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinaca, items) { public View getView(int position, View convertView, ViewGroup parent) { View v = super.getView(position, convertView, parent); Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/HelveticaNeueLTCom-Lt.ttf"); ((TextView) v).setTypeface(externalFont); return v; } public View getDropDownView(int position, View convertView, ViewGroup parent) { View v =super.getDropDownView(position, convertView, parent); Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/HelveticaNeueLTCom-Lt.ttf"); ((TextView) v).setTypeface(externalFont); v.setBackgroundColor(Color.GREEN); return v; } }; adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); 

可能需要添加

 import android.view.ViewGroup; 

到您文件顶部的导入列表。 出于某种原因,当Eclipse不能识别代码中涉及的ViewGroup类时,就不会提出这个build议。

这是对我有用 (使用CommonsWare和gsanllorente的答案):

 private static class MySpinnerAdapter extends ArrayAdapter<String> { // Initialise custom font, for example: Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/Blambot.otf"); // (In reality I used a manager which caches the Typeface objects) // Typeface font = FontManager.getInstance().getFont(getContext(), BLAMBOT); private MySpinnerAdapter(Context context, int resource, List<String> items) { super(context, resource, items); } // Affects default (closed) state of the spinner @Override public View getView(int position, View convertView, ViewGroup parent) { TextView view = (TextView) super.getView(position, convertView, parent); view.setTypeface(font); return view; } // Affects opened state of the spinner @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { TextView view = (TextView) super.getDropDownView(position, convertView, parent); view.setTypeface(font); return view; } } 

如果你像我一样,最初使用ArrayAdapter.createFromResource()和数组资源(如在Spinner文档中 )填充Spinner,那么你将使用MySpinnerAdapter,像这样:

 MySpinnerAdapter<String> adapter = new MySpinnerAdapter( getContext(), R.layout.view_spinner_item, Arrays.asList(getResources().getStringArray(R.array.my_array)) ); spinner.setAdapter(adapter); 

您可以通过自定义的SpinnerAdaptergetView()getDropDownView()应用字体。

如果您在另一个文件中实现适配器,则可以使用Adapter的构造函数访问“getAssets()”函数,因为您将Context作为参数。

 public class YourItemAdapter extends ArrayAdapter<String> { int recurso; Typeface tf; public YourItemAdapter(Context _context, int _resource, List<String> _items) { super(_context, _resource, _items); recurso=_resource; tf=Typeface.createFromAsset(_context.getAssets(),"font/digital-7.ttf"); } @Override public View getView(int position, View convertView, ViewGroup parent) { //You can use the new tf here. TextView spinner_text=(TextView)findViewById(R.id.text1); spinner_text.setTypeface(tf); } } 

试试这个创build自定义custom_spinner.xml

 <?xml version="1.0" encoding="utf-8"?> <com.xxxx.xxxx.CheckedTextViewC xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" style="?android:attr/spinnerDropDownItemStyle" android:singleLine="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:textAlignment="center" android:paddingTop="5dp" android:paddingBottom="5dp" android:textSize="18sp" /> 

像这样创build自定义的CheckedtextView

 import android.content.Context; import android.graphics.Typeface; import android.util.AttributeSet; import android.widget.CheckedTextView; public class CheckedTextViewC extends CheckedTextView { public CheckedTextViewC(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public CheckedTextViewC(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public CheckedTextViewC(Context context) { super(context); // TODO Auto-generated constructor stub } public void setTypeface(Typeface tf, int style) { if(!this.isInEditMode()){ Typeface normalTypeface = Typeface.createFromAsset(getContext().getAssets(), "font/Roboto-Light.ttf"); Typeface boldTypeface = Typeface.createFromAsset(getContext().getAssets(), "font/Roboto-Light.ttf"); if (style == Typeface.BOLD) { super.setTypeface(boldTypeface/*, -1*/); } else { super.setTypeface(normalTypeface/*, -1*/); } } } } 

实施新的布局

 adapter= new ArrayAdapter <String>(Menu.this,R.layout.custom_spinner, list); 

伙计们,我发现了一个很棒的解决scheme,我用helper来包装orignal适配器

使用这个类SpinnerViewHelper和开发与Android的快乐

 new SpinnerViewHelper((Spinner)view.findViewById(R.id.labelSurveyNumber),(parent, v, position, id) -> UrduFontHelper.set(v)); 

使用Lambdaexpression式。