简体   繁体   中英

How can I change the font of ListView?

I need some assistance on how to change my ListView font. I have searched and checked out some answers but I don't really understand it.

I have successfully changed the TextView by using the following code:

public class MyTextView extends TextView {

    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyTextView(Context context) {
        super(context);
        init();
    }

    public void init() {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/roboto.ttf");
        setTypeface(tf ,1);

    }

But I want to change the ListView element font, is there any way to do it like mentioned above?

you have to use a custom listView adapter. and you can change the font in that adapter. Check this site to know how to use custom adapter http://www.vogella.com/tutorials/AndroidListView/article.html

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
        Your_Class_Name.this,
        android.R.layout.simple_list_item_multiple_choice, Your_Array_List) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        CheckedTextView textView = (CheckedTextView) view
                .findViewById(android.R.id.text1);

        // You do here whatever you want with textView

        return view;
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM