android ListView中的自定义字体 [英] custom font in android ListView

查看:27
本文介绍了android ListView中的自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在整个应用程序中都使用了自定义字体(顺便说一句,我令人沮丧地发现您必须以编程方式手动应用到每个控件!),我需要将它应用到列表视图.问题是我看不到在哪里将列表字体中使用的 textview 设置为我的自定义字体(因为我从未实例化它 - 这一切都由适配器处理).

I'm using a custom font throughout my application (which, incidentally, I've frustratingly found out that you have to apply programmatically by hand to EVERY control!), and I need to apply it to a listview. The problem is that I can't see where I'd set the textview used in the list's font to my custom font (as I never instantiate it - that's all taken care of by the adapter).

我最希望的是能够使用这样的适配器:

What I'd ideally like is to be able to use an adapter like this:

new ArrayAdapter(Context context, TextView textView, List<T> objects)

那样我可以在填充我的列表之前做:textView.setTypeface.有谁知道是否有办法按照这些方式做一些事情?

That way I could do: textView.setTypeface before populating my list. Does anyone know if there's a way to do something along these lines?

推荐答案

你不能那样做,因为你传递给 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;
}

}

然后你可以这样设置

ListView lv = new ListView(this);
lv.setAdapter(new MyAdapter(objs));

希望这能让你继续前进.

Hopefully that should get you going.

这篇关于android ListView中的自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆