如何为ListView行文本设置自定义字体?

时间:2014-07-17 11:00:43

标签: android listview android-fragments

我有一个列表,其中包含从db检索的两个元素。 我也在使用片段...... 我想以自定义方式设置列表视图中每个行的文本样式。 我用于listView元素的textview的xml文件是: useful_numbers_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:id="@+id/useful_nums_h_layout_header">

    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/useful_nums_group_item"
            android:textSize="15dp"
            android:layout_gravity="left"/>

    <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" >
   </ListView>
</LinearLayout>

我想设置此textview的字体,如下所示:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View
 parent = inflater.inflate(R.layout.useful_numbers_fragment, container, false);
        Typeface t = Typeface.createFromAsset(getActivity().getAssets(), "fonts/ArialNarrow.ttf");
        TextView tv = (TextView) parent.findViewById(R.id.useful_nums_group_item);
        tv.setTypeface(t);
        return parent;
    }

但是我得到了 java.lang.RuntimeException:无法启动活动 引起:android.view.InflateException:二进制XML文件行#98:错误膨胀类片段 引起:java.lang.IllegalStateException:不能与自定义内容视图一起使用             在android.support.v4.app.ListFragment.setEmptyText(ListFragment.java:234) at ... onViewCreated()...

我用这种方式解决了这个问题:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = super.onCreateView(inflater, container, savedInstanceState);
        ViewGroup parent = (ViewGroup) inflater.inflate(R.layout.useful_numbers_fragment, container, false);
        parent.addView(v, 0);
        Typeface t = Typeface.createFromAsset(getActivity().getAssets(), "fonts/ArialNarrow.ttf");
        TextView tv = (TextView) parent.findViewById(R.id.useful_nums_group_item);
        tv.setTypeface(t);
        return parent;
    }

但是文字字体没有改变。

SimpleCursorAdapter.ViewBinder binder = new SimpleCursorAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                Typeface t = Typeface.createFromAsset(getActivity().getAssets(), "fonts/ArialNarrow.ttf");
                TextView tv=(TextView)view.findViewById(R.id.useful_nums_group_item);
                tv.setText(tv.getText());
                tv.setTextColor(Color.RED);
                tv.setTypeface(t);

                return true;
           }
        };
        simpleCursorAdapter.setViewBinder(binder);

1 个答案:

答案 0 :(得分:0)

创建一个自定义适配器类,在SimpleCursorAdapter集外部字体中扩展CustomAdapter

Fragment's onCreateView(...)

中进行一些更改
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View v= (View) inflater.inflate(R.layout.useful_numbers_fragment, null);
        Typeface t = Typeface.createFromAsset(getActivity().getAssets(), "fonts/ArialNarrow.ttf");
        TextView tv = (TextView) v.findViewById(R.id.useful_nums_group_item);
        tv.setTypeface(t);
        return v;
    }

代码段

public class CustomAdapter extends SimpleCursorAdapter {

            private Context mContext;
            private Context appContext;
            private int layout;
            private Cursor cr;
            private final LayoutInflater inflater;
            private Typeface t;

            public CustomAdapter(Context context,int layout, Cursor c,String[] from,int[] to) {
                super(context,layout,c,from,to);
                this.layout=layout;
                this.mContext = context;
                this.inflater=LayoutInflater.from(context);
                this.cr=c;
                t = Typeface.createFromAsset(context.getAssets(), "fonts/ArialNarrow.ttf");
            }

            @Override
            public View newView (Context context, Cursor cursor, ViewGroup parent) {
                    return inflater.inflate(layout, null);
            }

            @Override
            public void bindView(View view, Context context, Cursor cursor) {
                super.bindView(view, context, cursor);
                TextView titleS=(TextView)view.findViewById(R.id.TitleSong);
                TextView artistS=(TextView)view.findViewById(R.id.Artist);
                titleS.setTypeface(t);
                artistS.setTypeface(t);
                int Title_index=cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
                int Artist_index=cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);

                titleS.setText(cursor.getString(Title_index));
                artistS.setText(cursor.getString(Artist_index));

            }

    }