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

查看:417
本文介绍了在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?

推荐答案

您不能这样做的,因为你传递给一个Ar​​rayAdapter文本视图资源是在每次使用时夸大了。

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天全站免登陆