custom font in android ListView

后端 未结 8 703
清歌不尽
清歌不尽 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: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

提交回复
热议问题