android ListView中的自定义字体

时间:2011-01-02 00:06:06

标签: android listview fonts typeface

我在我的应用程序中使用自定义字体(顺便说一下,我很惊讶地发现你必须手动以编程方式应用于每个控件!),我需要将它应用到列表视图中。问题是我无法看到我将列表字体中使用的textview设置为自定义字体的位置(因为我从未实例化它 - 所有这些都由适配器处理)。

我理想的是能够使用这样的适配器:

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

这样我可以在填充列表之前执行:textView.setTypeface。有谁知道是否有办法沿着这些方向做某事?

8 个答案:

答案 0 :(得分:9)

如果您不想创建新类,可以在创建适配器时覆盖getView方法,这是带有标题和副标题的simpleAdapter的示例:

Typeface typeBold = Typeface.createFromAsset(getAssets(),"fonts/helveticabold.ttf");
Typeface typeNormal = Typeface.createFromAsset(getAssets(), "fonts/helvetica.ttf");

SimpleAdapter adapter = new SimpleAdapter(this, items,R.layout.yourLvLayout, new String[]{"title",
    "subtitle" }, new int[] { R.id.rowTitle,
    R.id.rowSubtitle }){
            @Override
        public View getView(int pos, View convertView, ViewGroup parent){
            View v = convertView;
            if(v== null){
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v=vi.inflate(R.layout.yourLvLayout, null);
            }
            TextView tv = (TextView)v.findViewById(R.id.rowTitle);
            tv.setText(items.get(pos).get("title"));
            tv.setTypeface(typeBold);
            TextView tvs = (TextView)v.findViewById(R.id.rowSubtitle);
            tvs.setText(items.get(pos).get("subtitle"));
            tvs.setTypeface(typeNormal);
            return v;
        }


};
listView.setAdapter(adapter);

其中items是地图的ArrayList

希望有所帮助

答案 1 :(得分:8)

您无法这样做,因为每次使用时,传递给ArrayAdapter的文本视图资源都会被充气。

您需要创建自己的适配器并提供自己的视图。

适配器的示例可能是

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));

希望这能让你前进。

答案 2 :(得分:2)

看起来构造函数错误

将其更改为:

public MyAdapter (Context context, List<Object> objects) {
    this.context = context;
    this.objects = objects;
}

对我来说效果很好。

答案 3 :(得分:2)

尝试使用arrayadapters ::

Typeface typeNormal = Typeface.createFromAsset(getAssets(), "roboto_lite.ttf");

timearray = new ArrayAdapter<String>(DetailsActivity.this,R.layout.floorrow,R.id.txt, flor) {
    public View getView(int pos, View convertView, android.view.ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.floorrow, null);
        }
        TextView tv = (TextView)v.findViewById(R.id.txt);
        tv.setText(flor.get(pos));
        tv.setTypeface(typeNormal);
        return v;
    }; 
};

lv_building.setAdapter(timearray);

答案 4 :(得分:1)

除了MoisésOlmedo的反应之外 - 一种不创造新课程的替代变体:

    tf = Typeface.createFromAsset(getAssets(), fontPath);

    recordsAdapter = new SimpleCursorAdapter(this, R.layout.item1, cursor, from, to);

    recordsAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            if (columnIndex == 1) {
                final TextView tv = (TextView) view;
                tv.setTypeface(tf);
            }
            return false;
        }
    });

答案 5 :(得分:0)

首先将字体文件复制并粘贴到assets / fonts文件夹中。 然后识别文本视图。

    Typeface font=Typeface.createFromAsset(activity.getAssets(), "fonts/<font_file_name>.ttf");
    holder.text.setTypeface(font);
    holder.text.setText("your string variable here");

答案 6 :(得分:0)

您可以设置基本适配器,如下步骤可能会帮助您

定义基本适配器

public class LessonAdapter extends BaseAdapter {

    private Context mContext;

    public LessonAdapter(Context mContext, ArrayList<String> titles) {
        super();
        this.mContext = mContext;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        if (titles!=null)
            return titles.size();
        else
            return 0;
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = convertView;
        try
        {
            if (v == null) {
                v = inflater.inflate(R.layout.lesson_item, null);
            }
                TextView title = (TextView) v.findViewById(R.id.title);

                Typeface tf = Typeface.createFromAsset(getAssets(),
                        "fonts/Rabiat_3.ttf");
                title.setTypeface(tf);

                title.setText(titles.get(position).toString());     

        }
        catch (Exception e) {
            Log.d("searchTest", e.getMessage());
        }

        return v;
    }

}

通过TypeFace方法,您可以通过在Assets中添加文件夹'Fonts'来设置字体,然后在'Fonts'文件夹中添加您的字体

然后设置你的适配器

adapter = new LessonAdapter(LessonsTitle.this, titles);
    setListAdapter(adapter);

答案 7 :(得分:0)

holder.txt_name.setTypeface(Typeface.createFromAsset(activity.getAssets(),&#34; font / nasimbold.ttf&#34;));