更改listview行的字体

时间:2012-03-25 07:38:10

标签: java android fonts typeface

我想更改ListView的每一行的字体。我的listview是一个自定义列表视图,其中每一行都是textview,我想通过此代码设置该textview的facetype:

   Typeface font = Typeface.createFromAsset(getAssets(), "fonts/titr.ttf");  
   mainlisttext.setTypeface(font); 

但是在运行时我得到错误,这是我的LogCat:

    03-25 11:46:02.242: W/dalvikvm(5841): threadid=1: thread exiting with uncaught exception (group=0x40a4a1f8)
03-25 11:46:02.281: E/AndroidRuntime(5841): FATAL EXCEPTION: main
03-25 11:46:02.281: E/AndroidRuntime(5841): java.lang.RuntimeException: Unable to start activity ComponentInfo{Dic.proj.pkg/Dic.proj.pkg.DictionaryActivity}: java.lang.NullPointerException
03-25 11:46:02.281: E/AndroidRuntime(5841):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
03-25 11:46:02.281: E/AndroidRuntime(5841):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
03-25 11:46:02.281: E/AndroidRuntime(5841):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
03-25 11:46:02.281: E/AndroidRuntime(5841):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
03-25 11:46:02.281: E/AndroidRuntime(5841):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 11:46:02.281: E/AndroidRuntime(5841):     at android.os.Looper.loop(Looper.java:137)
03-25 11:46:02.281: E/AndroidRuntime(5841):     at android.app.ActivityThread.main(ActivityThread.java:4424)
03-25 11:46:02.281: E/AndroidRuntime(5841):     at java.lang.reflect.Method.invokeNative(Native Method)
03-25 11:46:02.281: E/AndroidRuntime(5841):     at java.lang.reflect.Method.invoke(Method.java:511)
03-25 11:46:02.281: E/AndroidRuntime(5841):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-25 11:46:02.281: E/AndroidRuntime(5841):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-25 11:46:02.281: E/AndroidRuntime(5841):     at dalvik.system.NativeStart.main(Native Method)
03-25 11:46:02.281: E/AndroidRuntime(5841): Caused by: java.lang.NullPointerException
03-25 11:46:02.281: E/AndroidRuntime(5841):     at Dic.proj.pkg.DictionaryActivity.onCreate(DictionaryActivity.java:152)
03-25 11:46:02.281: E/AndroidRuntime(5841):     at android.app.Activity.performCreate(Activity.java:4465)
03-25 11:46:02.281: E/AndroidRuntime(5841):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-25 11:46:02.281: E/AndroidRuntime(5841):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

在独立的textview中,这很好用,但对于listview里面的textview不行!我怎么能解决这个问题?

2 个答案:

答案 0 :(得分:0)

从路径中删除字体文件夹

Typeface font = Typeface.createFromAsset(getAssets(), "titr.ttf");
mainlisttext.setTypeface(font); 

答案 1 :(得分:0)

您的TextView 主列表文本在此处为空,您无法为该列表行指定自定义字体。您可以做的是实现您的自定义适配器(您需要适配器的活动中的内部类)并使用您的新字体自定义所有行(您没有说明您使用的适配器,因此我使用ArrayAdapter<String> ,相同的代码可以应用于其他类型):

private class CustomAdapter extends ArrayAdapter<String> {

    Typeface font;

    public CustomAdapter(Context context, int textViewResourceId,
            List<String> objects) {
        super(context, textViewResourceId, objects);
        font = Typeface.createFromAsset(getAssets(),
                "fonts/Roboto-Italic.ttf");
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //  Note: this will work if your row layout is a single TextView with no other views in it,
        // if you have LinearLayouts or other views then search by id in the view returned by super.getView(position, convertView, parent) 
        TextView text = (TextView) super.getView(position, convertView,
                parent);
        text.setTypeface(font);
        return text;
    }

}