问题描述
我的资产文件夹中有一个 ttf 字体文件.我知道如何将它用于文本视图:
I have a ttf font file in my assets folder. I know how to use it for textviews with:
Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/HelveticaNeueLTCom-Lt.ttf");
textview1.setTypeface(externalFont);
我已经在它自己的 xml 文件中定义了我的微调文本的查找(就像在 android 中一样):
I have defined look for my spinner text in it's own xml file (as usuall in 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" />
我只是不能从代码中引用这个文本视图,我总是得到空指针异常.例如.我试过了:
I just can't reference this textview from code, i always get null pointer exceptions. E.g. i tried:
TextView spinner_text=(TextView)findViewById(R.id.text1);
spinner_text.setTypeface(externalFont);
是否可以为我自己的 xml 中定义的微调文本选择我的外部字体?
Is it possible to select my external font even for my spinner text defined in it's own 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 类时,它不会提出此建议.
To your list of imports at the top of your file. For some reason Eclipse doesn't make this suggestion when it doesn't recognize the ViewGroup class involved in the code.
推荐答案
这对我有用(使用来自 CommonsWare's 和 gsanllorente 的 答案):
This is what worked for me (using ideas both from CommonsWare's and gsanllorente's answers):
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 文档),然后你会像这样使用 MySpinnerAdapter:
If you, like me, originally populated the Spinner using ArrayAdapter.createFromResource()
and an array resource (as in Spinner documentation), then you'd use MySpinnerAdapter like this:
MySpinnerAdapter<String> adapter = new MySpinnerAdapter(
getContext(),
R.layout.view_spinner_item,
Arrays.asList(getResources().getStringArray(R.array.my_array))
);
spinner.setAdapter(adapter);
这篇关于如何以编程方式将字体自定义字体设置为 Spinner 文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!