java - 在 Android 按钮中应用自定义字体

标签 java android xml android-layout fonts

我正在尝试向我的 Android 按钮添加自定义 roboto 字体;然而,大多数解决方案在这种情况下都不起作用。

我正在尝试使用这里的解决方案... Setting Button text font in android

我的资源文件夹中有字体。

enter image description here

这是我的代码 fragment :

  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)

不知道为什么当它适用于其他人时不能为我制作原生字体......?

最佳答案

创建一个 asset 目录并将您的 customefont.ttf 文件放入该目录中..

    Button bu = (Button) findViewById(R.id.custom_font); 
    Typeface    font=Typeface.createFromAsset(getAssets(),"yourCustomeFont.ttf"); 
    bu.setTypeface(font);

这种方式适用于特定按钮但是如果你想为整个应用程序应用这种字体,你可以使用这种方式应用这个库在你的gradle文件中编译它complie'me.anwarshahriar:calligrapher:1.0'

之后还剩下 2 个步骤。

1- 在主 Activity 的 onCreate 方法中实例化 calligrapher 并设置字体

  Calligrapher calligrapher = new Calligrapher(this);       
  calligrapher.setFont(this, "yourCustomFontHere.ttf", true);

如果您想让整个应用程序访问此字体,则无需执行步骤 1,而是在扩展应用程序类的类中实例化 Calligraphy,如下所示:

  CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
            .setDefaultFontPath("fonts/yourCustomFontHere.ttf")
            .setFontAttrId(R.attr.fontPath)
            .build()
    );

2-通过重写主 Activity 中的 AttachBaseContext 方法将其注入(inject)上下文,如下所示:

  @Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

这是最优雅、超快速的方法..

关于java - 在 Android 按钮中应用自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24225432/

相关文章:

java - Android游戏3供用户试用

java - JAXB 解码 - 未找到具有默认根元素的描述符...

android - 错误的 xml 或 android 兼容性问题?

java - Kotlin中如何从泛型继承接口(interface)?

java - ITextPdf : Printing Arabic strings from Right To Left (RTL)

Java文件输入和输出

android - 如何开始Android/iOS配件开发?

android - Fresco - 来自 ControllerListener 的 onIntermediateImageSet 未被调用

android - 如何正确安装 admob,在某些测试手机上不显示

xml - 报表标题与页面内容的表格重叠