【发布时间】:2014-12-09 19:07:40
【问题描述】:
我正在使用 Android Studio 创建一个 Android 应用。 我在使用自定义 SimpleAdapter 的活动中有列表视图。 我需要在自定义适配器中使用自定义字体,但是当我尝试时它不起作用。 没有错误,只有没有使用字体。直接在活动中使用时,字体路径可以正常工作。
当我注销创建的字体时,我得到了这个:
E/====﹕ FONT: android.graphics.Typeface@4c5dfbc0
这是我的自定义适配器代码:
package com.myapp.app.utilities;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import com.fieldly41.app.R;
import java.util.ArrayList;
import java.util.HashMap;
public class SimpleIconAdapter extends SimpleAdapter {
private ArrayList<HashMap<String, String>> results;
//private Context context;
Typeface font;
public SimpleIconAdapter(Context context, ArrayList<HashMap<String, String>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
this.results = data;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
View v = view;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item_icon, null);
}
if(results.get(position) != null ) {
Typeface fonter = Typeface.createFromAsset(v.getResources().getAssets(), "fonts/ss-symbolicons-line.ttf");
TextView top_label = (TextView) v.findViewById(R.id.top_label);
TextView icon_label = (TextView) v.findViewById(R.id.icon);
TextView bottom_label = (TextView) v.findViewById(R.id.bottom_label);
icon_label.setText("????");
icon_label.setTypeface(fonter);
if (results.get(position).get("locked").equals("false")) {
icon_label.setTextColor(Color.WHITE);
} else {
icon_label.setTextColor(Color.RED);
}
top_label.setText(results.get(position).get("title"));
bottom_label.setText(results.get(position).get("created_at"));
}
return v;
}
}
【问题讨论】:
-
不要从视图中获取字体 从上下文中获取它会像 Typeface.createFromAsset(context.getAssets(), "fontname.ttf");
-
不要在适配器中设置自定义字体,而是使用字体创建自定义 TextView。