custom font in android ListView

后端 未结 8 605
清歌不尽
清歌不尽 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条回答
  • 2020-11-29 06:37

    Try like this for 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);
    
    0 讨论(0)
  • 2020-11-29 06:38

    If you don't want to create a new class you can override the getView method when creating your Adapter, this is an example of a simpleAdapter with title and subtitle:

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

    where items is your ArrayList of Maps

    hope that helps

    0 讨论(0)
  • 2020-11-29 06:39

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

    0 讨论(0)
  • 2020-11-29 06:47

    In addition to the response of Moisés Olmedo - an alternative variant without creating a new class:

        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;
            }
        });
    
    0 讨论(0)
  • 2020-11-29 06:47

    You can Set base Adapter like follow Steps May be help you

    Define your Base Adapter

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

    by TypeFace Method you can set your font by add folder 'Fonts' in Assets then add your font in 'Fonts' Folder

    then to set your adapter

    adapter = new LessonAdapter(LessonsTitle.this, titles);
        setListAdapter(adapter);
    
    0 讨论(0)
  • 2020-11-29 06:48

    Looks like the constructor is wrong

    change it to:

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

    it worked well for me.

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