【问题标题】:How can i set a custom font on my listview?如何在我的列表视图上设置自定义字体?
【发布时间】:2012-12-10 23:49:46
【问题描述】:

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

【问题讨论】:

  • 将自定义字体设置为列表视图中的文本视图
  • 我想我不完全理解你的问题。你想在哪里应用你的字体?如果您使用自定义 ListAdapter,在 public View getView(int position, View convertView, ViewGroup parent) {...} 方法中应用字体应该很容易。
  • 使用自定义列表适配器并在“getView”覆盖中设置字体。 softwarepassion.com/…
  • @Eulante 为了清楚起见,ListView 中的每一行/条目都包含在 TextView 中(因此它是 TextView 的列表)。因此,您的目标是获取每个 TextView 并在其上设置字体样式(我不知道能够为整个 ListView 设置“全局”)。您可以在 ListViewAdapter 中执行此操作。如果您在下面查看我的答案(第二个答案),您会看到这是如何完成的。如果您需要 Java 代码中的 sn-p,请告诉我,但翻译非常简单。现在明白了吗?
  • 这里,这其实是一个重复的问题,看看答案:stackoverflow.com/questions/7361135/…

标签: android listview sdk fonts


【解决方案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-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
  • 2016-08-15
  • 1970-01-01
相关资源
最近更新 更多