2

我的问题是如何在android的自定义列表视图中更改印地语字体。我正在使用此代码 Typeface 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 方法但无法工作。请帮助我。

4

2 回答 2

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));
于 2012-10-08T08:59:07.050 回答
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;
}
};
于 2012-10-08T08:49:50.263 回答