2012-10-08 29 views
2

我的問題是,如何更改字體在安卓自定義列表視圖中的印地文我使用此代碼 字體hindiFont = Typeface.createFromAsset(getAssets(),「fonts/DroidHindi.ttf 「); //添加的菜單項到ListView控件如何設置印地文字體在列表視圖

 ListAdapter adapter = new SimpleAdapter(this, menuItems, 
       R.layout.list_item, 
       new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] { 
         R.id.name, R.id.desciption, R.id.cost }); 
     // Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/DroidHindi.ttf"); 

     setListAdapter(adapter); 

     // selecting single ListView item 
     ListView lv = getListView(); 

請幫助我。 在此先感謝!!!!!!!!!

我重寫了getView方法,但無法work.please幫助我。在SimpleAdapter類的

回答

1

覆蓋getView方法,並設置字體的TextView的,要更改字體,如下:

ListAdapter adapter = new SimpleAdapter(this, menuItems, 
       R.layout.list_item, 
       new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] { 
         R.id.name, R.id.desciption, R.id.cost }){ 

public View getView(View view) 
{ 
     LayoutInflater inflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE); 
    Typeface hindiFont=Typeface.createFromAsset(getAssets(),"fonts/DroidHindi.ttf"); 
    LinearLayout llMain=(LinearLayout)inflater.inflate(R.layout.list_item, null); 
    TextView txtName=(TextView)llMain.findViewById(R.id.name); 
    txtName.setTypeface(hindiFont); 
    TextView txtDescription=(TextView)llMain.findViewById(R.id.desciption); 
    txtDescription.setTypeface(hindiFont); 
    return llMain; 
} 
}; 
2

資產的文件夾放印地文TrueType字體文件

並創建自定義適配器

public class MyArrayAdapter extends ArrayAdapter<String> { 
    private final Context context; 
    private final String[] values; 

    public MyArrayAdapter(Context context, String[] values) { 
     super(context, R.layout.list_mobile, values); 
     this.context = context; 
     this.values = values; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View rowView = inflater.inflate(R.layout.list_mobile, parent, false); 

     TextView textView = (TextView) rowView.findViewById(R.id.label); 

     //here use your true type font of hindi like this hindiFonts.ttf is placed inside asset/fonts/ folder of project 
     Typeface face=Typeface.createFromAsset(getAssets(), "fonts/hindiFonts.ttf"); 
     textView .setTypeface(face); 
     textView.setText(values[position]); 


     return rowView; 
    } 
} 

一套以上的自定義適配器列表視圖的

String[] MOBILE_OS = 
       new String[] { "Android", "iOS", "WindowsMobile", "Blackberry"}; 


     myListView.setListAdapter(new MyArrayAdapter(this, MOBILE_OS)); 
相關問題