如何在android custombaseadapter上设置字体类型

时间:2014-04-26 01:12:05

标签: android fonts

我想在列表视图中更改字体类型, 我按照this回答,

我声明就像这样

 lv = (ListView)findViewById(R.id.listView1);
customadapter.CustomBaseAdapter adapter = 
new customadapter.CustomBaseAdapter(this, rowItems, "Van Helsing.ttf");
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);

但我的listview字体没有任何变化,它不会改变。

这是我的自定义适配器:

public CustomBaseAdapter(Context context, List<RowItem> items, String FONT) {
    this.context = context;
    this.rowItems = items;
    tf = Typeface.createFromAsset(context.getAssets(), FONT);
}

与我的代码人员有什么关系?

编辑:

Context context;
List<RowItem> rowItems;
Typeface tf;


public CustomBaseAdapter(Context context, List<RowItem> items, String FONT) {
    this.context = context;
    this.rowItems = items;
    tf = Typeface.createFromAsset(context.getAssets(), FONT);
}

/*private view holder class*/
private class ViewHolder {

    TextView txtMenu;


}

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

    LayoutInflater mInflater = (LayoutInflater) 
        context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.txtMenu = (TextView) convertView.findViewById(R.id.namaMenu);

        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    RowItem rowItem = (RowItem) getItem(position);

    holder.txtMenu.setText(rowItem.getNamaMenu()); 
    holder.textMenu.setTypeFace(tf);

    return convertView;
}

@Override
public int getCount() {     
    return rowItems.size();
}

@Override
public Object getItem(int position) {
    return rowItems.get(position);
}

@Override
public long getItemId(int position) {
    return rowItems.indexOf(getItem(position));
}

我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/namaMenu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#CC0033"
    android:textSize="32dp"
    android:textAppearance="?android:attr/textAppearanceMedium" />

1 个答案:

答案 0 :(得分:1)

您需要将字体设置为列表项的Textview

 textView.setTypeface(tf);

tf是自定义字体的Typeface实例

实施例

 @Override
public View getView(int position, View v, ViewGroup parent)
{
    View mView = v ;
    if(mView == null){
        LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = vi.inflate(id, null);
    }

    TextView text = (TextView) mView.findViewById(R.id.textView);
    textView.setTypeface(tf);
  }
}