Android系统。为ListView的每个项目设置不同的字体

时间:2014-07-27 11:36:10

标签: android listview android-listview fonts

我有一个ListView.I想要为ListView的每个项目设置不同的字体。谁可以帮我一个例子? 我想要这样的东西http://www.ulduzsoft.com/wp-content/uploads/2012/01/font-preference-android-emulator-150x150.png

1 个答案:

答案 0 :(得分:1)

在列表适配器的getView

中执行类似的操作
public class MyAdapter extends ArrayAdapter<JSONObject> {
    Context mContext;
    private LayoutInflater inflater;

    public MyAdapter(Context context, List<JSONObject> list) {
        super(context, 0, list);
        this.mContext = context;
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        final Typeface type1 = Typeface.createFromAsset(getAssets(), "myFancyFont.ttf");
        final Typeface type2 = Typeface.createFromAsset(getAssets(), "DroidSans.ttf");

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_feed, parent, false);

            holder = new ViewHolder();
            holder.textview = (TextView) convertView.findViewById(R.id.label_title);


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

       if(position == 0) {
           holder.textview.setTypeface(type1);
       } else {
           holder.textview.setTypeface(type2);
       }
       holder.textview.setText("item with pos." + position)
       return convertView;
    }

    class ViewHolder {
        TextView textivew;
    }
}

另外,您可以将字体定义为静态最终字体。