Android为listview项目使用自定义字体

Android为listview项目使用自定义字体,android,textview,typeface,Android,Textview,Typeface,您好,我需要一些帮助,使用自定义字体的listview项目。我从资产文件夹中获取字体,如下所示: Typeface font = Typeface.createFromAsset(getAssets(), "hermesbgbold.otf"); 我正试图为我的listview项设置它,但问题是我正在为我的listview使用SimpleAdapter,而TextView在另一个XML中,我正在使用它作为列表视图的contentView。以下是更好理解的代码: public void onC

您好,我需要一些帮助,使用自定义字体的listview项目。我从资产文件夹中获取字体,如下所示:

Typeface font = Typeface.createFromAsset(getAssets(), "hermesbgbold.otf"); 
我正试图为我的listview项设置它,但问题是我正在为我的listview使用SimpleAdapter,而TextView在另一个XML中,我正在使用它作为列表视图的contentView。以下是更好理解的代码:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.events);
// code

SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.events_items,
                new String[]{ICON,TITLE, INFO}, new int[]{ R.id.thumb,R.id.title, R.id.dates})

}

因此,我想在自定义字体中使用的文本视图位于
events\u items.xml
中。那么我如何设置
标题
日期
来使用这种自定义字体呢?

创建您自己的自定义适配器并为文本设置字体

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // ViewHolder will buffer the assess to the individual fields of the row
    // layout

    ViewHolder holder;
    // Recycle existing view if passed as parameter
    // This will save memory and time on Android
    // This only works if the base layout for all classes are the same
    View rowView = convertView;
    if (rowView == null) {

        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.main_listview, null, true);

        holder = new ViewHolder();
        holder.textView = (TextView) rowView.findViewById(R.id.main_name);
        rowView.setTag(holder);

    } else {
        holder = (ViewHolder) rowView.getTag();
    }

    Typeface font = Typeface.createFromAsset(getAssets(), "hermesbgbold.otf"); 
    holder.textView.setTypeface(font);
    holder.textView.setText(title);

    return rowView;
}

创建您自己的自定义适配器并为文本设置字体

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // ViewHolder will buffer the assess to the individual fields of the row
    // layout

    ViewHolder holder;
    // Recycle existing view if passed as parameter
    // This will save memory and time on Android
    // This only works if the base layout for all classes are the same
    View rowView = convertView;
    if (rowView == null) {

        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.main_listview, null, true);

        holder = new ViewHolder();
        holder.textView = (TextView) rowView.findViewById(R.id.main_name);
        rowView.setTag(holder);

    } else {
        holder = (ViewHolder) rowView.getTag();
    }

    Typeface font = Typeface.createFromAsset(getAssets(), "hermesbgbold.otf"); 
    holder.textView.setTypeface(font);
    holder.textView.setText(title);

    return rowView;
}