0

我有一个类扩展到我想为其设置自定义字体的 ListActivity。任何人都可以帮我完成这个吗?我知道如何使用 TextView 的字体,但不知道如何使用 ListActivity。

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();
            }
        }
    }
4

1 回答 1

2

AListView本身没有任何文本可以为其自定义字体。您需要处理提供此文本的类:Adapter. 您的适配器负责创建ListView包含(在这种情况下)有TextView问题的视图。您需要创建一个自定义适配器(可能从 派生ArrayAdapter<String>),您可以在其中覆盖该getView方法。最简单的解决方案可能是简单地调用父实现getView,然后使用(您可以查找它,但我相信相关的 id 是)TextViewandroid.R.layout.simple_list_item_1布局中找到相关的,然后设置您已经知道如何的自定义字体去做。这可能看起来像:View.findViewByIdtext1

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
    }
}
于 2013-08-05T23:52:42.310 回答