custom font in android ListView

后端 未结 8 606
清歌不尽
清歌不尽 2020-11-29 06:15

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!), an

相关标签:
8条回答
  • First copy and paste the font files into assets/fonts folder. Then identify the textview.

        Typeface font=Typeface.createFromAsset(activity.getAssets(), "fonts/<font_file_name>.ttf");
        holder.text.setTypeface(font);
        holder.text.setText("your string variable here");
    
    0 讨论(0)
  • 2020-11-29 07:00

    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.

    0 讨论(0)
提交回复
热议问题