我正在尝试将自定义roboto字体添加到我的android按钮;但是,大多数解决方案在这种情况下都不起作用。
我正在尝试使用此处的解决方案...... Setting Button text font in android
我的资源文件夹中有字体。
以下是我的代码片段:
Button block_block_font =(Button) findViewById(R.id.block_button);
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Light.ttf");
block_block_font.setText("show");
block_block_font.setTypeface(typeface);
Button security_settings_button_font =(Button) findViewById(R.id.security_settings_button);
security_settings_button_font.setText("show");
security_settings_button_font.setTypeface(typeface);
Button blacklist_whitelist_button_font =(Button) findViewById(R.id.blacklist_whitelist_button);
blacklist_whitelist_button_font.setText("show");
blacklist_whitelist_button_font.setTypeface(typeface);
但是,当我应用该解决方案进行编辑时,我产生了这个错误:
06-14 16:59:56.677 26389-26389/com.spicycurryman.getdisciplined10.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spicycurryman.getdisciplined10.app/com.spicycurryman.getdisciplined10.app.MainActivity}: java.lang.RuntimeException: native typeface cannot be made
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
at android.app.ActivityThread.access$700(ActivityThread.java:152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: native typeface cannot be made
at android.graphics.Typeface.<init>(Typeface.java:307)
at android.graphics.Typeface.createFromAsset(Typeface.java:281)
at com.spicycurryman.getdisciplined10.app.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:5250)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
at android.app.ActivityThread.access$700(ActivityThread.java:152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
不确定为什么当它适用于其他人时,无法为我制作原生字体??
答案 0 :(得分:2)
创建一个assets目录并将您的customefont.ttf文件放在该目录中..
Button bu = (Button) findViewById(R.id.custom_font);
Typeface font=Typeface.createFromAsset(getAssets(),"yourCustomeFont.ttf");
bu.setTypeface(font);
这种方式适用于特定按钮但是如果你想为整个应用程序应用这个字体你可以使用这种方式应用这个lib在你的gradle文件中编译它complie'me.anwarshahriar:calligrapher:1.0'
并在主要活动
中的onCreate方法中使用它 Calligrapher calligrapher = new Calligrapher(this);
calligrapher.setFont(this,“yourCustomFontHere.ttf”,true);
这是最优雅的超快速方式..
答案 1 :(得分:0)
查看截图,看起来您的资源文件夹位于src文件夹中。
将资产移至项目根目录并试一试。
答案 2 :(得分:0)
将所有字体直接放在资源文件夹中,并在开始时更改为引用没有文件夹名称的资产名称,然后尝试。
或强> 删除src中的assets文件夹并将其直接放到app / main。