android - android ListView 中的自定义字体

标签 android listview fonts typeface

我在整个应用程序中使用自定义字体(顺便说一句,我沮丧地发现您必须以编程方式手动将其应用到每个控件!),并且我需要将它应用到 ListView 。问题是我看不到我在哪里将列表字体中使用的 TextView 设置为我的自定义字体(因为我从未实例化它 - 这一切都由适配器处理)。

我最理想的是能够使用这样的适配器:

new ArrayAdapter(Context context, TextView textView, List<T> objects)

这样我可以做:textView.setTypeface 在填充我的列表之前。有谁知道是否有办法按照这些思路做某事?

最佳答案

如果您不想创建新类,您可以在创建适配器时覆盖 getView 方法,这是一个带有标题和副标题的简单适配器示例:

Typeface typeBold = Typeface.createFromAsset(getAssets(),"fonts/helveticabold.ttf");
Typeface typeNormal = Typeface.createFromAsset(getAssets(), "fonts/helvetica.ttf");

SimpleAdapter adapter = new SimpleAdapter(this, items,R.layout.yourLvLayout, new String[]{"title",
    "subtitle" }, new int[] { R.id.rowTitle,
    R.id.rowSubtitle }){
            @Override
        public View getView(int pos, View convertView, ViewGroup parent){
            View v = convertView;
            if(v== null){
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v=vi.inflate(R.layout.yourLvLayout, null);
            }
            TextView tv = (TextView)v.findViewById(R.id.rowTitle);
            tv.setText(items.get(pos).get("title"));
            tv.setTypeface(typeBold);
            TextView tvs = (TextView)v.findViewById(R.id.rowSubtitle);
            tvs.setText(items.get(pos).get("subtitle"));
            tvs.setTypeface(typeNormal);
            return v;
        }


};
listView.setAdapter(adapter);

items 是你的 map ArrayList

希望对你有帮助

关于android - android ListView 中的自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4576441/

相关文章:

android - 如何在我的应用程序中添加 android 菜单溢出图标

android - 如果我将我的 Android 应用程序设置为系统应用程序,在恢复出厂设置后,它会从手机中删除吗?

listview - 从 Flutters ListView 中删除项目只会删除最后一个小部件

windows - 远东语言文本不会在 Windows Compact 7 中绘制

android - Android 主屏幕小部件是否支持 TextView 中的背景选择器?

android - 在 Canvas 上移动位图

android - 在 Android 中跳过 getView

android - 为什么 Google 在 Gmail 应用对话 ListView 中使用 Canvas?

html - 如何在 CSS 中设置按钮字体的样式?

java - 如何将自定义字体与 Slick 的 UnicodeFont 类一起使用?