我正在尝试更改 a 的字体TextView
in my ArrayAdapter
。字体chantelli_antiqua.ttf
位于资产文件夹中。
这是我的 Java 代码:
listItemAdapter = new ArrayAdapter<MenuItem>(this, R.layout.listitem, menuItems);
Typeface font = Typeface.createFromAsset(getAssets(), "chantelli_antiqua.ttf");
TextView v = (TextView)listItemAdapter.getView(0, null, null);
v.setTypeface(font);
列表项布局的 xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="30sp"
/>
我很确定问题出在Adapter.getView(int, View, ViewGroup)
方法。我只是不太明白要作为变量传递什么并尝试过null
。但这并没有达到我想要的效果。
如何更改字体TextView
in the Adapter
到自定义字体?
Update
根据 Pixie 的建议我创建了一个MenuItemAdapter
这延伸了ArrayAdapter<MenuItem>
:
public class MenuItemAdapter extends ArrayAdapter<MenuItem>
{
private Typeface font;
public MenuItemAdapter(Context context, int textViewResourceId, List<MenuItem> objects)
{
super(context, textViewResourceId, objects);
font = Typeface.createFromAsset(context.getAssets(), "chantelli_antiqua.ttf");
}
@Override
public View getView(int position, View view, ViewGroup viewGroup)
{
((TextView)view).setTypeface(font);
return super.getView(position, view, viewGroup);
}
}
并将我的java代码更改为:
listItemAdapter = new MenuItemAdapter(this, R.layout.listitem, menuItems);
但现在我的应用程序崩溃了onCreate
of the ListActivity
,但在到达断点之前getView(...)
,我还没能弄清楚为什么。有什么建议吗?