Java 更改listview行的字体
我想更改ListView中每一行的字体。My listview是一个自定义listview,其中的每一行都是textview,我希望通过以下代码设置该textview的facetype:Java 更改listview行的字体,java,android,fonts,typeface,Java,Android,Fonts,Typeface,我想更改ListView中每一行的字体。My listview是一个自定义listview,其中的每一行都是textview,我希望通过以下代码设置该textview的facetype: Typeface font = Typeface.createFromAsset(getAssets(), "fonts/titr.ttf"); mainlisttext.setTypeface(font); 但在运行时,我得到一个错误,这是我的日志: 03-25 11:46:02.
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/titr.ttf");
mainlisttext.setTypeface(font);
但在运行时,我得到一个错误,这是我的日志:
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
mainlisttext在此处为空,您不能以这种方式为列表行指定自定义字体。您可以实现自定义适配器(活动中需要适配器的内部类)并使用新字体自定义所有行(您没有说明使用的适配器,因此我使用ArrayAdapter
,相同的代码可以应用于其他类型):
私有类CustomAdapter扩展了ArrayAdapter{
字体;
公共CustomAdapter(上下文上下文,int textViewResourceId,
列出对象){
超级(上下文、textViewResourceId、对象);
font=Typeface.createFromAsset(getAssets(),
“字体/Roboto Italic.ttf”);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//注意:如果您的行布局是一个单一的文本视图,而其中没有其他视图,则此选项将起作用,
//如果您有LinearLayouts或其他视图,则在super.getView(位置、转换视图、父视图)返回的视图中按id搜索
TextView text=(TextView)super.getView(position,convertView,
父母);
text.setTypeface(字体);
返回文本;
}
}
能否发布DictionaryActivity类的第152行?这就是它崩溃的地方NPE来自字典活动。onCreate
,而不是调用setTypeface
。
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;
}
}