本文介绍了android中listview中的自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在android中的listview中使用自定义字体?
How to use custom font in listview in android?
推荐答案
答案说明:
你可以这样做是因为每次使用时传递给ArrayAdapter的文本视图资源都会膨胀。
您需要创建自己的适配器并提供您自己的观点。
您的适配器的示例可能是
You can''t do it that way because the text view resource you pass to the ArrayAdapter is inflated each time it is used.
You need to create your own adapter and provide your own view.
An example for your adapter could be
public class MyAdapter extends BaseAdapter {
private List<Object> objects; // obviously don't use object, use whatever you really want
private final Context context;
public CamAdapter(Context context, List<Object> objects) {
this.context = context;
this.objects = objects;
}
@Override
public int getCount() {
return objects.size();
}
@Override
public Object getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Object obj = objects.get(position);
TextView tv = new TextView(context);
tv.setText(obj.toString()); // use whatever method you want for the label
// set whatever typeface you want here as well
return tv;
}
}
然后你就可以这样设定
And then you could set that as such
ListView lv = new ListView(this);
lv.setAdapter(new MyAdapter(objs));
希望这能让你前进。
Hopefully that should get you going.
这篇关于android中listview中的自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!