2013-08-05 84 views
0

我有一个扩展到我想设置自定义字体的ListActivity的类。任何人都可以通过这个来引导我吗?我知道如何使用TextView的字体,但不知道如何使用ListActivity。Android中ListView的自定义字体?

public static class PunjabiBooks extends ListActivity{ 

    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     String[] mathList = new String[] {"Gurmukh Soch", "Books (Punjabi)", "Books (Hindi)", "Books (English)", 
     "Annual Report"}; 
     setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mathList)); 

    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     // TODO Auto-generated method stub 
     super.onListItemClick(l, v, position, id); 
     Object o = this.getListAdapter().getItem(position); 
     String keyword = o.toString(); 
     if (keyword.equals("Gurmukh Soch")){ 
      x = 0; 
      try{ 
       Class ourClass = Class.forName("sukrit.trust.PublicationsTab$WebActivity"); 
       Intent ourIntent = new Intent(PunjabiBooks.this, ourClass); 
       startActivity(ourIntent); 
      }catch(ClassNotFoundException e){ 
       e.printStackTrace(); 
      } 
     }else if(keyword.equals("Books (Punjabi)")){ 
      try{ 
       Class ourClass = Class.forName("sukrit.trust.Publications$PunjabiBooks"); 
       // Intent ourIntent = new Intent(Publications.this, ourClass); 
       //startActivity(ourIntent); 
      }catch(ClassNotFoundException e){ 
       e.printStackTrace(); 
      } 
     }else if(keyword.equals("Books (Hindi)")){ 
      try{ 
       Class ourClass = Class.forName("sukrit.trust.PublicationsTab$HindiBooks"); 
       //     Intent ourIntent = new Intent(Publications.this, ourClass); 
       //     startActivity(ourIntent); 
      }catch(ClassNotFoundException e){ 
       e.printStackTrace(); 
      } 
     }else if(keyword.equals("Books (English)")){ 
      try{ 
       Class ourClass = Class.forName("sukrit.trust.PublicationsTab$EnglishBooks"); 
       //     Intent ourIntent = new Intent(Publications.this, ourClass); 
       //     startActivity(ourIntent); 
      }catch(ClassNotFoundException e){ 
       e.printStackTrace(); 
      } 
     }else if(keyword.equals("Annual Report")){ 
      try{ 
       Class ourClass = Class.forName("sukrit.trust.PublicationsTab$AnnualReport"); 
       //     Intent ourIntent = new Intent(Publications.this, ourClass); 
       //     startActivity(ourIntent); 
      }catch(ClassNotFoundException e){ 
       e.printStackTrace(); 
      } 
     } 
    } 

回答

2

A ListView本质上没有任何文本可以定制字体。您需要处理提供此文本的类:Adapter。您的适配器负责在ListView中创建视图,包括(在本例中)TextView。您需要创建一个自定义适配器(可能来自ArrayAdapter<String>),您可以在其中覆盖getView方法。最简单的解决方案可能是简单地调用父实现的getView,然后从android.R.layout.simple_list_item_1布局中使用View.findViewById(您可以查看它,但我相信相关的ID是text1),然后从自定义字体中设置相关的TextView你已经知道该怎么做。这可能看起来像这样:

public class CustomAdapter extends ArrayAdapter<String> { 
    // Appropriate Constructor here 
    // ... 

    public View getView (int position, View convertView, ViewGroup parent) { 
     View view = super.getView(position, convertView, parent); 
     TextView textView = (TextView) view.findViewById(android.R.id.text1); 
     // Do your textView.setTypeface(...) stuff here 
    } 
}