【问题标题】:Applying custom fonts in buttons for android在 android 的按钮中应用自定义字体
【发布时间】:2014-08-05 04:48:31
【问题描述】:

我正在尝试将自定义机器人字体添加到我的 android 按钮;但是,大多数解决方案在这种情况下都不起作用。

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

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

这是我的代码的 sn-p:

  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)

不确定为什么原生字体适用于其他人时无法为我制作...?

【问题讨论】:

  • 您尝试使用的特定字体文件似乎有问题。
  • AFAIK 在资产中创建自己的文件夹目前尚不受 Android 支持。
  • 我不知道是不是这样,因为这可以在另一个活动中使用我的文本视图(即字体)
  • 是的,继续研究,我错了。 (虽然这适用于可绘制对象。)
  • 并且,在研究中发现了这个potential duplicate

标签: java android xml android-layout fonts


【解决方案1】:

创建一个 assets 目录并将您的 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 = new Calligrapher(this);       
  calligrapher.setFont(this, "yourCustomFontHere.ttf", true);

如果你想让这个字体被整个应用程序访问,那么代替步骤 1 来实例化类中的书法,它像这样扩展应用程序类:

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

2- 通过在主 Activity 中重写 attachBaseContext 方法,将其注入上下文:

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

这是最优雅的超快速方式..

【讨论】:

    【解决方案2】:

    查看您的屏幕截图,您的资产文件夹似乎在您的 src 文件夹中。

    将资产移至项目根目录并尝试一下。

    【讨论】:

      【解决方案3】:

      把你所有的字体直接放到assets文件夹里,改成一开始就引用asset名字不带文件夹名,然后试试。

      或者 把src里面的assets文件夹去掉,直接放到app/main里面。

      【讨论】:

        猜你喜欢
        • 2014-12-29
        • 1970-01-01
        • 1970-01-01
        • 2014-02-06
        • 2023-03-07
        • 2016-08-08
        • 2014-01-27
        • 1970-01-01
        • 2011-02-27
        相关资源
        最近更新 更多