首页 > 解决方案 > 如何在网格视图中为字符串数组设置自定义字体

问题描述

我似乎无法弄清楚如何为字符串数组设置自定义字体。

String quizname[] = {"Cricket", "Football", "Tennis", "Golf", "Rugby", `"Hardcore",};`

我知道如何为这样的文本视图设置自定义字体,但不确定如何在网格视图中设置字符串:

    Typeface custom_font = Typeface.createFromAsset(getAssets(), "slant.TTF");
    prefs = getSharedPreferences("appPurchase", 0);
    editor = prefs.edit();
    textView.setTypeface(custom_font);
    textView.setTextColor(getResources().getColor(R.color.gold));

    custGridList = new ArrayList<>();

    for (int i = 0; i < quizname.length; i++) {
        CustGridList list = new CustGridList();
        list.setQuizImg(imgs[i]);
        list.setQuizName(quizname[i]);
        custGridList.add(list);
    }

标签: javaandroid

解决方案


onBindViewHolder

您可以在示例中的 recyclerView 项目中设置字体:

class Holder extends RecyclerView.ViewHolder {

        TextView t;

        public Holder(View itemView) {
            super(itemView);
            t = itemView.findViewById(R.id.text);
        }
    }

在你的onBindViewHolder

@Override
public void onBindViewHolder(@NonNull Holder holder, int position) {
      holder.t.setTypeface(custom_font);
}

推荐阅读