java - 自定义 ArrayAdapter 中未出现字体

标签 java android android-arrayadapter

我有这个自定义阵列适配器:

public class CustomAdapter extends ArrayAdapter<DataObj> 
{
int                     layoutResourceId;
List<DataObj>   data;
Activity                activity;
Typeface                iconFont;

public CustomAdapter(Activity activity, int layoutResourceId, List<DataObj> data)
{
    super(activity, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.activity = activity;
    this.data = data;
    iconFont = Typeface.createFromAsset(activity.getAssets(), "icons.ttf" );
}

public static class ViewHolder
{
    public TextView icon;
    public TextView name;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    View row = convertView;
    ViewHolder holder;

    if (row == null) 
    {
        LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.event_type_list_row, null);
        holder = new ViewHolder();
        holder.icon = (TextView) row.findViewById(R.id.layout_icon_EditText);
        holder.name = (TextView) row.findViewById(R.id.layout_name_EditText);
        row.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) row.getTag();
    }
    final DataObj obj = data.get(position);
    if (eventType != null) 
    {
        holder.icon.setText(obj.getIconCode());
        holder.icon.setTypeface(iconFont);
        holder.name.setText(obj.getName());
    }

    return row;
}

}

我看到的不是图标,而是代码(我使用 IcoMoon 作为我的图标字体)。可以看到我的应用程序中静态创建的其他图标,但在这里我看到了这个: enter image description here 我的代码有什么问题吗?

这是静态时它的工作原理(它不是图像,而是 IcoMoon 的字体): enter image description here

注意:我尝试使用其他字体(不是图标字体)并且它有效,似乎只有图标字体不起作用。

最佳答案

为什么你不尝试:

TextView text = new TextView();

String str = "some text";
SpannableStringBuilder sb = new SpannableStringBuilder(str);
            sb.setSpan(iconFont, 0, str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            text.setTextSize(18);
            text.setText(sb);

关于java - 自定义 ArrayAdapter 中未出现字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21439707/

相关文章:

java - 如何存储1000行及之后执行批处理?

java - 使用Spring Cloud实现一个全局锁

android - 如何在parse.com中获取插入行的objectId

android - ListFragment 中没有加载圆圈

jvm - OrientDB更新遇到JVM内存问题 'Cannot allocate memory'

java - 将属性文件加载到 java.util.Properties?

android - 对于同一个位图,位图有时保存为黑色图像

android - 屏幕旋转期间处理状态变化的最佳实践

android - 将 ArrayAdapter 与 AlertDialog 和 .setAdapter 结合使用

android - 微调器的数组适配器不能在 fragment 中工作,需要资源 ID