如何在getView方法中为列表项使用字体

时间:2016-05-31 07:52:58

标签: android listview exception

我尝试了很多次和许多解决方案,但每次我发现异常或任何其他错误,不应用字体或给我本机类型不能成为错误。 我在下面给出了我的代码,请告诉我我做错了什么:

    public class AttractionsAdapter extends ArrayAdapter {

        private List<Attractions> mENList;
        private int mResource;
        private LayoutInflater inflater;

        Typeface headingStyle ;
        Typeface contentStyle ;

public AttractionsAdapter(Context context, int resource, List<Attractions>       objects) {
            super(context, resource, objects);

      mResource = resource;
      mENList = objects;
      inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);

            //Setting styles for the list items..
            headingStyle = Typeface.createFromAsset(context.getAssets(), "Attr_List_heading.ttf");
            contentStyle = Typeface.createFromAsset(context.getAssets(), "Attr_Content.ttf");`
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;

            if(convertView == null){

                holder = new ViewHolder();
                convertView = inflater.inflate(mResource, null);

   holder.mTitle = (TextView)convertView.findViewById(R.id.heading_1);
   holder.mSummary = (TextView)convertView.findViewById(R.id.summary_1);

                holder.mTitle.setTypeface(headingStyle);
                holder.mSummary.setTypeface(contentStyle);

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

1 个答案:

答案 0 :(得分:1)

现在,您的代码假设字体位于/assets目录中。实际上,您将所有字体放在/assets/fonts/yourfont.ttf中。您应该是这样的,

 headingStyle = Typeface.createFromAsset(context.getAssets(), "fonts/Attr_List_heading.ttf");
 contentStyle = Typeface.createFromAsset(context.getAssets(), "fonts/Attr_Content.ttf");

我希望这对你有用。