如何更改ListView的字体?

时间:2014-04-10 10:00:17

标签: android listview fonts

我需要一些有关如何更改ListView字体的帮助。 我已经搜索并查看了一些答案,但我并不理解它。

我已使用以下代码成功更改了TextView:

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

    }

但我想更改ListView元素字体,有没有办法像上面提到的那样做?

2 个答案:

答案 0 :(得分:0)

您必须使用自定义listView适配器。并且您可以更改该适配器中的字体。检查此站点以了解如何使用自定义适配器http://www.vogella.com/tutorials/AndroidListView/article.html

答案 1 :(得分:0)

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;
    }
};
相关问题