2011-10-30 72 views
1

嗨我需要一點幫助,使用自定義字體的列表視圖項目。我得到我的字體從資產文件夾是這樣的:Android使用自定義字體的列表視圖項目

Typeface font = Typeface.createFromAsset(getAssets(), "hermesbgbold.otf"); 

和我想要將其設置爲我的列表視圖項目,但問題是,我使用的是SimpleAdapter我的ListView和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}) 

} 

所以,TextView的的,我想用自定義的字體使用的是events_items.xml。那麼我如何設置titledates來使用這種自定義字體呢?

+1

您必須使用customAdapter –

回答

1

創建您自己的自定義適配器並設置文本的字體。

@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; 
} 
相關問題