我的问题是如何在android中的自定义列表视图中更改hindi中的字体。我正在使用此代码 字体hindiFont = Typeface.createFromAsset(getAssets(),“fonts / DroidHindi.ttf”); //将menuItems添加到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方法,但无法正常工作。请帮助我。
答案 0 :(得分:2)
将hindi 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));
答案 1 :(得分:1)
覆盖SimpleAdapter类的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;
}
};