gpt4 book ai didi

android - 如何为 TextView xml 布局设置字体

转载 作者:太空狗 更新时间:2023-10-29 12:44:26 26 4
gpt4 key购买 nike

我创建了一个名称为 def_list 的 XML 布局。我为它分配了一个 ID,即 def_list_textview。我在 Assets 文件夹中放置了一个名为 bn.ttf 的字体。

XML 布局如下:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/def_list_textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right" >
</TextView>

但是当我使用代码为其设置字体时:

def_list_tv = (TextView)findViewById(R.id.def_list_textview);
Typeface tf = Typeface.createFromAsset(this.getAssets(), "fonts/bn.ttf");
def_list_tv.setTypeface(tf);

我在 LogCat 中得到一个 nullPointerException。我不知道为什么。

这是我的 LogCat:

12-26 17:14:56.496: E/AndroidRuntime(1254): FATAL EXCEPTION: main
12-26 17:14:56.496: E/AndroidRuntime(1254): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.adeveloper.handydic/net.adeveloper.dic.PortDef}: java.lang.NullPointerException
12-26 17:14:56.496: E/AndroidRuntime(1254): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-26 17:14:56.496: E/AndroidRuntime(1254): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-26 17:14:56.496: E/AndroidRuntime(1254): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-26 17:14:56.496: E/AndroidRuntime(1254): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-26 17:14:56.496: E/AndroidRuntime(1254): at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 17:14:56.496: E/AndroidRuntime(1254): at android.os.Looper.loop(Looper.java:123)
12-26 17:14:56.496: E/AndroidRuntime(1254): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-26 17:14:56.496: E/AndroidRuntime(1254): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 17:14:56.496: E/AndroidRuntime(1254): at java.lang.reflect.Method.invoke(Method.java:507)
12-26 17:14:56.496: E/AndroidRuntime(1254): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-26 17:14:56.496: E/AndroidRuntime(1254): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-26 17:14:56.496: E/AndroidRuntime(1254): at dalvik.system.NativeStart.main(Native Method)
12-26 17:14:56.496: E/AndroidRuntime(1254): Caused by: java.lang.NullPointerException
12-26 17:14:56.496: E/AndroidRuntime(1254): at net.adeveloper.dic.PortDef.onCreate(PortDef.java:53)
12-26 17:14:56.496: E/AndroidRuntime(1254): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-26 17:14:56.496: E/AndroidRuntime(1254): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-26 17:14:56.496: E/AndroidRuntime(1254): ... 11 more

最佳答案

问题可能出在

this.getAssets()

getAssets() 是来自 Context 的方法,因此尝试传递上下文对象而不是 this

尝试

Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/bn.ttf");

Typeface tf = Typeface.createFromAsset(mContext.getAssets(), "fonts/bn.ttf");

这里mContext是Context的对象

编辑:

请检查以下自定义 TextView 是否可以帮助您

public class CustomTextView extends TextView{
public CustomTextView(Context context,AttributeSet attrs,int defStyle){
super(context,attrs,defStyle);
init();
}
public CustomTextView(Context context,AttributeSet attrs){
super(context,attrs);
init();
}
public CustomTextView(Context context){
super(context);
init();
}

private void init(){
if(!isInEditMode()){
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/bn.ttf");
setTypeface(tf);
}
}
}

我在我之前的一个项目中使用过这个类

关于android - 如何为 TextView xml 布局设置字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20785877/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com