4

我无法在列表视图上设置字体,字体仅适用于文本视图。有什么方法可以轻松设置自定义字体?谢谢你。

4

1 回答 1

-1

您可以为列表视图创建一个适配器,并在那里的 TextView 上设置字体类型。

这是一个适配器的剪辑(它在 C# 中,但与 Java 没有太大区别):

    class ParcelRecordContactAdapter : ArrayAdapter<ParcelRecordContact>
    {
        List<ParcelRecordContact> contacts;

        public ParcelRecordContactAdapter(Context context, int textViewResourceId, List<ParcelRecordContact> contacts)
            : base(context, textViewResourceId, contacts)
        {
            this.contacts = contacts;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View v = convertView;

            if (v == null)
            {
                LayoutInflater li = (LayoutInflater)this.Context.GetSystemService(Context.LayoutInflaterService);

                v = li.Inflate(Resource.Layout.ListItem_SingleLine, null);
            }

            ParcelRecordContact contact = contacts[position];

            if (contact != null)
            {
                /**********************************************************
                 * change font on tv here
                 **********************************************************/

                TextView tv = (TextView)v.FindViewById(Resource.Id.tv_single_line_list_item_1);

                if (tv != null)
                {                        
                    tv.Text = "android is fu.... n"

                    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/DroidSansFallback.ttf");

                    tv.setTypeface(tf);
                }
            }

            return v;
        }
    }
于 2012-12-24T15:58:11.747 回答