[英]change font of a row of 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則不行! 我該如何解決?
從路徑中刪除字體文件夾
Typeface font = Typeface.createFromAsset(getAssets(), "titr.ttf");
mainlisttext.setTypeface(font);
您的TextView
主列表文本在那里為null,您不能通過這種方式為列表行分配自定義字體。 您可以做的是實現您的自定義適配器(活動中需要適配器的內部類),並使用新字體自定義所有行(您沒有說要使用什么適配器,所以我使用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.