我试图在我的ArrayAdapter
中更改TextView
的字体。字体chantelli_antiqua.ttf
位于资产文件夹中。ArrayAdapter中textview的自定义字体
这里是我的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
。但这并不符合我的愿望。
如何将Adapter
中的TextView
的字体更改为自定义字体?
更新
根据精灵的建议,我创建了一个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
的
ListActivity
,但在击中
getView(...)
的断点之前,我还没有能够弄清楚为什么。任何建议?
UPDATE2
改变了代码的getView(...)来:
@Override
public View getView(int position, View view, ViewGroup viewGroup)
{
View v = super.getView(position, view, viewGroup);
((TextView)v).setTypeface(font);
return v;
}
和工作原理。 :)
[应用自定义字体使用布局](http://androidtrainningcenter.blogspot.in/2013/07/applying-custom-font-in-entire-android.html) – Sameer 2013-07-12 05:46:27