自定义字体的TextView在ArrayAdapter [英] custom font for textview in ArrayAdapter

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

问题描述

我试图改变一个的TextView 的字体在我的 ArrayAdapter 。字体 chantelli_antiqua.ttf 是资产的文件夹。

I'm trying to change the font of a TextView in my ArrayAdapter. The font chantelli_antiqua.ttf is in the assets folder.

<打击> 这里是我的Java code:

Here is my Java code:

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 for the listitem layout:

<?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,查看,ViewGroup中)方法。我真的不明白传递的变量,并试图。但是,这并没有做什么,我想它。

I'm quite sure the problem lies with the Adapter.getView(int, View, ViewGroup) method. I just don't really understand what to pass as variables and tried null. But this doesn't do what I would like it to.

如何修改的TextView 的字体在适配器的自定义字体?

How to change the font of the TextView in the Adapter to the custom font?

根据精灵的建议,我创建了一个 MenuItemAdapter 延伸 ArrayAdapter&LT;菜单项&GT;

According to Pixie's suggestion I created a MenuItemAdapter which extends 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 code到:

And changed my java code to:

listItemAdapter = new MenuItemAdapter(this, R.layout.listitem, menuItems);

但现在我在的onCreate 的应用程序崩溃的 ListActivity ,但命中断点之前 getView(...),我一直无法找出尚未原因。任何建议?

But now my app crashes after the onCreate of the ListActivity, but before hitting the breakpoint in getView(...), I haven't been able to figure out yet why. Any suggestion?

改变了$ C $下getView(...)为:

Changed the code for getView(...) to:

@Override  
public View getView(int position, View view, ViewGroup viewGroup)
{
 View v = super.getView(position, view, viewGroup);
 ((TextView)v).setTypeface(font);
 return v;
}

和工作原理。 :)

推荐答案

您应该不叫你的适配器 getView()方法。该的ListView 为您完成此。你必须扩展 ArrayAdapter 类和重写 getView()方法来代替。在这种方法中,你必须抬高一个新的视图或再使用 convertView ,并设置字体为这一观点。

You shouldn't call the getView() method of your adapter. The ListView does this for you. You have to extend the ArrayAdapter class and override the getView() method instead. In this method you have to inflate a new view or re-use convertView and set the typeface for this view.

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

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