簡體   English   中英

如何更改ListView的字體?

[英]How can I change the font of ListView?

我需要一些有關如何更改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元素的字體,有沒有辦法像上面提到的那樣呢?

您必須使用自定義listView適配器。 您可以在該適配器中更改字體。 檢查此站點以了解如何使用自定義適配器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;
    }
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM