简体   繁体   English

更改列表视图的行的字体

[英]change font of a row of listview

I want to change the typeface of every row of a ListView. 我想更改ListView每行的字体。 My listview is a custom listview that every row of that is a textview and i want to set facetype of that textview by this code: 我的列表视图是一个自定义列表视图,该列表的每一行都是一个textview,我想通过以下代码设置该textview的facetype:

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

But at runtime i get error, This is my LogCat: 但是在运行时我得到了错误,这是我的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)

In a independent textview this works fine, But for a textview inside listview not! 在独立的textview中,这很好用,但是对于listview中的textview则不行! How i can fix that? 我该如何解决?

Remove the font folder from the path 从路径中删除字体文件夹

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

Your TextView mainlisttext is null there, you can't assign a custom font for your list rows that way. 您的TextView 主列表文本在那里为null,您不能通过这种方式为列表行分配自定义字体。 What you could do is implement your custom adapter(a inner class in the activity where you need the adapter) and customize all the rows with your new font(you didn't say what adapter you use so I use a ArrayAdapter<String> , the same code could be applied for other types): 您可以做的是实现您的自定义适配器(活动中需要适配器的内部类),并使用新字体自定义所有行(您没有说要使用什么适配器,所以我使用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;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM