如何在自定义BaseAdapter中使用自定义字体

我正在使用Android Studio创建Android应用。 我在使用自定义SimpleAdapter的活动中有listview。 我需要在自定义适配器中使用自定义字体,但是当我尝试它时不起作用。 没有错误只有没有使用字体。 直接在活动中使用时,字体路径可以正常工作。

当我注销创建的fonter时,我得到了这个:

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> results; //private Context context; Typeface font; public SimpleIconAdapter(Context context, ArrayList<HashMap> 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; } } 

你的实现接近确定。

但是最大的问题是你在getView()方法中创建了一个非常耗费资源的TypeFace实例。

因为每当滚动列表时, getView()方法会重复调用N次。

并且从资产中大量加载资源是不好的做法,它可能随时导致OutOfMemoryError

所以我的建议是创建公共对象并在getView()中使用。

 public SimpleIconAdapter(Context context, ArrayList> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); font=Typeface.createFromAsset(context.getAssets(), "fonts/ss-symbolicons-line.ttf"); this.results = data; } 

在getView()中删除此行

 Typeface fonter = Typeface.createFromAsset(v.getResources().getAssets(), "fonts/ss-symbolicons-line.ttf"); 

而使用“font”对象代替fonter

 icon_label.setTypeface(font); 

试试这种方式,希望这可以帮助您解决问题。

由于它是一个列表视图,我建议你创建一个自定义textview并将其放在行布局xml中。

注意:将所需的字体文件放在assets文件夹中非常重要。

使用自定义字体创建自定义TextView。

CustomTextView.java

 public class CustomTextView extends TextView { public CustomTextView(Context context) { super(context); setFont(); } public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); setFont(); } public CustomTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setFont(); } private void setFont() { Typeface font = Typeface.createFromAsset(getContext().getAssets(),"fonts/ss-symbolicons-line.ttf"); setTypeface(font, Typeface.NORMAL); } } 

尝试定义自定义TextView而不是简单的TextView,您希望显示自定义字体。

list_item_icon.xml

      

关注自定义TextView声明和初始化,并在使用自定义适配器时尝试使用ViewHolder概念。

 public class SimpleIconAdapter extends SimpleAdapter { private Context context; private ArrayList> results; public SimpleIconAdapter(Context context, ArrayList> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); this.results = data; this.context=context; } @Override public View getView(int position, View view, ViewGroup parent) { ViewHolder holder; if (view == null) { holder = new ViewHolder(); view = LayoutInflater.from(context).inflate(R.layout.list_item_icon, null); holder.top_label = (TextView) view.findViewById(R.id.top_label); holder.icon_label = (CustomTextView) view.findViewById(R.id.icon); holder.bottom_label = (TextView) view.findViewById(R.id.bottom_label); view.setTag(holder); }else{ holder = (ViewHolder) view.getTag(); } holder.icon_label.setText("💀"); if (results.get(position).get("locked").equals("false")) { holder.icon_label.setTextColor(Color.WHITE); } else { holder.icon_label.setTextColor(Color.RED); } holder.top_label.setText(results.get(position).get("title")); holder.bottom_label.setText(results.get(position).get("created_at")); return view; } class ViewHolder{ TextView top_label; CustomTextView icon_label; TextView bottom_label; } }