如何设置ListView的行内文本自定义字体? [英] How to set custom font for ListView row text?

查看:316
本文介绍了如何设置ListView的行内文本自定义字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个要素,我从数据库中检索列表。
我也使用碎片...
欲样式每一行中一个自定义的方式在列表视图中的文本。
xml文件在那里我有我使用的ListView控件元素是TextView的:
useful_numbers_fragment.xml

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
              机器人:方向=垂直
              机器人:layout_width =match_parent
              机器人:layout_height =match_parent
              机器人:ID =@ + ID / useful_nums_h_layout_header>    <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:ID =@ + ID / useful_nums_group_item
            机器人:TEXTSIZE =15dp
            机器人:layout_gravity =左/>    < ListView控件
            机器人:ID =@机器人:ID /列表
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_centerHorizo​​ntal =真正的>
   < /&的ListView GT;
< / LinearLayout中>

我要将此TextView的只是像这样的字样:

  @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){        视图
 父= inflater.inflate(R.layout.useful_numbers_fragment,集装箱,FALSE);
        字样T = Typeface.createFromAsset(getActivity()getAssets(),字体/ ArialNarrow.ttf);
        TextView的电视=(TextView中)parent.findViewById(R.id.useful_nums_group_item);
        tv.setTypeface(T);
        回到父母;
    }

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

我解决这样问题:

  @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
        视图V = super.onCreateView(充气器,容器,savedInstanceState);
        ViewGroup中父=(ViewGroup中)inflater.inflate(R.layout.useful_numbers_fragment,集装箱,FALSE);
        parent.addView(V,0);
        字样T = Typeface.createFromAsset(getActivity()getAssets(),字体/ ArialNarrow.ttf);
        TextView的电视=(TextView中)parent.findViewById(R.id.useful_nums_group_item);
        tv.setTypeface(T);
        回到父母;
    }

但文字的字体没有改变。

  SimpleCursorAdapter.ViewBinder粘结剂=新SimpleCursorAdapter.ViewBinder(){
            @覆盖
            公共布尔setViewValue(查看视图,光标光标,诠释参数:columnIndex){
                字样T = Typeface.createFromAsset(getActivity()getAssets(),字体/ ArialNarrow.ttf);
                TextView的电视=(TextView中)view.findViewById(R.id.useful_nums_group_item);
                tv.setText(tv.getText());
                tv.setTextColor(Color.RED);
                tv.setTypeface(T);                返回true;
           }
        };
        simpleCursorAdapter.setViewBinder(粘合剂);


解决方案

创建延伸 SimpleCursorAdapter 在自定义适配器类的 CustomAdapter 设置外部字体。

请一些变化片段的onCreateView(...)

  @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
        super.onCreateView(充气器,容器,savedInstanceState);
        视图V =(查看)inflater.inflate(R.layout.useful_numbers_fragment,NULL);
        字样T = Typeface.createFromAsset(getActivity()getAssets(),字体/ ArialNarrow.ttf);
        TextView的电视=(TextView中)v.findViewById(R.id.useful_nums_group_item);
        tv.setTypeface(T);
        返回伏;
    }

code段

 公共类CustomAdapter扩展SimpleCursorAdapter {            私人语境mContext;
            私人语境appContext;
            私人诠释布局;
            私人光标CR,
            私人最终LayoutInflater吹气;
            私人字样吨;            公共CustomAdapter(上下文的背景下,INT布局,光标C,的String []从,INT []到){
                超(背景下,布局,C,从,到);
                this.layout =布局;
                this.mContext =背景;
                this.inflater = LayoutInflater.from(上下文);
                this.cr = C;
                T = Typeface.createFromAsset(context.getAssets(),字体/ ArialNarrow.ttf);
            }            @覆盖
            公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){
                    返回inflater.inflate(布局,NULL);
            }            @覆盖
            公共无效bindView(查看视图,上下文的背景下,光标光标){
                super.bindView(查看,背景,光标);
                TextView的标题=(TextView中)view.findViewById(R.id.TitleSong);
                TextView的艺术家=(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));            }    }

I have a list with two elements I retrieve from db. I am also using fragments … I want to style the text of each the rows in the list view in a custom way. The xml file where I have the textview that I use for the listView elements is: 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>

I want to set the typeface of this textview just like this :

@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;
    }

but I get a java.lang.RuntimeException: Unable to start activity Caused by: android.view.InflateException: Binary XML file line #98: Error inflating class fragment Caused by: java.lang.IllegalStateException: Can't be used with a custom content view at android.support.v4.app.ListFragment.setEmptyText(ListFragment.java:234) at …onViewCreated()...

I resolved the problem in this way:

@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;
    }

But the text font hasn’t changed.

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

解决方案

Create a custom adapter class which extends SimpleCursorAdapter in your CustomAdapter set external typeface.

Make some changes in 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;
    }

Code snippet

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

            }

    }

这篇关于如何设置ListView的行内文本自定义字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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