列表视图与类型面

时间:2015-11-28 14:32:40

标签: android listview typeface

ListView listView = (ListView) findViewById(R.id.listView);
View v = listView.getChildAt(0);
Typeface typeface = Typeface.createFromAsset(getAssets(), "a.ttf");
((TextView) v).setTypeface(typeface);

它有什么问题?

1 个答案:

答案 0 :(得分:1)

您不能像这样设置自定义字体列表视图。首先,您需要为列表视图创建自己的自定义适配器。您需要在自定义适配器的getView方法中设置自定义字体。例如:

 @Override
 public View getView(int position,  View convertView, ViewGroup parent)
 {
     View v = convertView;
     if(v == null)
     {
        LayoutInflater li = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.list_item, null);
     }

     // Setting custom font to text view
     TextView titleText = (TextView) v.findViewById(R.id.title);
     Typeface typeface = Typeface.createFromAsset(getAssets(), "a.ttf");
     titleText.setTypeface(typeface);

     // Do other stuffs

    return v;
}