为 ListView 中的 TextViews 设置多个不同的字体 [英] Setting multiple different Typefaces to the TextViews in a ListView

查看:41
本文介绍了为 ListView 中的 TextViews 设置多个不同的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示 TextView 列表的 ListView.我希望每个 TextView 都以适当的字体显示.出现在 ListView 中作为 fonts String[] 数组一部分的字体名称的拼写与创建 Typeface 时相同.

I have a ListView that displays a list of TextViews. I want each TextView to be displayed in their appropriate typeface. The names of the fonts which appear in the ListView as part of the fonts String[] array are spelled the same as when the Typeface is created.

    ListView fontsListView = (ListView) this.view.findViewById(R.id.MenuLayout);
    final String[] fonts = new String[] {
            "Aclonica",
            "Amino-Regular",
    };
    ArrayList<String> fontsList = new ArrayList<String>();
    fontsList.addAll( Arrays.asList(fonts) );
    ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(getActivity(), R.layout.fonts_simple_row, fontsList);
        final TextView[] textViewArray = new TextView[0];
        for( int i = 0; i <= fonts.length - 1; i++) {
            textViewArray[i].setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/" + fonts[i] + ".ttf"));
        }
    fontsListView.setAdapter(listAdapter);

还有 XML:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:id="@+id/rowTextView"
android:textSize="20sp"
android:gravity="center_horizontal"
android:padding="10dp"
android:typeface="sans"
android:fontFamily="sans-serif-light" />

为什么我不能让 ListView 中的每个 TextView 都有正确的 Typface?

Why can't I get each TextView in the ListView its proper Typface?

推荐答案

ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.fonts_simple_row,value) {
@Override
 public View getView(int position, View convertView,ViewGroup parent) {
     View view = super.getView(position, convertView, parent);
     TextView textView = (TextView) view.findViewById(R.id.rowTextView); 
     textView.setTypeface(Typeface.createFromAsset(getActivity().getAssets(),"fonts/" + fonts[position] + ".ttf"));
     return view;
 }
 };

fontsListView.setAdapter(arrayAdapter);

这篇关于为 ListView 中的 TextViews 设置多个不同的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆