简体   繁体   中英

change font of a row of listview

I want to change the typeface of every row of a 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:

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

But at runtime i get error, This is my 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! 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. 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):

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;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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