简体   繁体   English

Android应用程序和ActionBar中的自定义字体

[英]Custom font in Android app and ActionBar

I've been busting my head all afternoon on implementing a custom font in my Android app. 我整个下午都在为自己的Android应用程序实现自定义字体而烦恼。 First, I had troubles with a custom font in my navigation drawer. 首先,我在导航抽屉中使用自定义字体时遇到了麻烦。 That doesn't seem to work. 这似乎不起作用。 So I decided to try the Actionbar title, but I'm having equal problems. 因此,我决定尝试使用Actionbar标题,但是遇到了同样的问题。

I found this website http://www.tristanwaddington.com/2013/03/styling-the-android-action-bar-with-a-custom-font/ and decided to try it. 我找到了该网站http://www.tristanwaddington.com/2013/03/styling-the-android-action-bar-with-a-custom-font/,并决定尝试一下。

I created a custom java file with the code below: 我使用以下代码创建了一个自定义Java文件:

package vimen.vimenlogin;

import android.content.Context;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;
import android.util.LruCache;

public class TypefaceSpan extends MetricAffectingSpan {
    /** An <code>LruCache</code> for previously loaded typefaces. */
    private static LruCache<String, Typeface> sTypefaceCache =
    new LruCache<String, Typeface>(12);

    private Typeface mTypeface;

    /**
     * Load the {@link Typeface} and apply to a {@link}.
     */
    public TypefaceSpan(Context context, String typefaceName) {
        mTypeface = sTypefaceCache.get(typefaceName);

        if (mTypeface == null) {
            mTypeface = Typeface.createFromAsset(context.getApplicationContext()
                .getAssets(), String.format("fonts/%s", typefaceName));

            // Cache the loaded Typeface
            sTypefaceCache.put(typefaceName, mTypeface);
        }
    }

    @Override
    public void updateMeasureState(TextPaint p) {
        p.setTypeface(mTypeface);

        // Note: This flag is required for proper typeface rendering
        p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }

    @Override
    public void updateDrawState(TextPaint tp) {
        tp.setTypeface(mTypeface);

        // Note: This flag is required for proper typeface rendering
        tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }
}

Afther that, I put the method in my main.java file: 除此之外,我将方法放在main.java文件中:

public void restoreActionBar()
{
    actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);

    SpannableString s = new SpannableString("testing");
    s.setSpan(new TypefaceSpan(this, "fonts/Raleway-Bold.ttf"), 0, s.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    // Update the action bar title with the TypefaceSpan instance
    actionBar.setTitle(s);
    //actionBar.setTitle(mTitle);
}

However, I'm getting this error Caused by: java.lang.RuntimeException: native typeface cannot be made coupled with a crashing app. 但是,出现此错误的Caused by: java.lang.RuntimeException: native typeface cannot be made与崩溃的应用程序结合使用。

TypefaceSpan does not take a font path. TypefaceSpan不采用字体路径。 It only works with the built-in font families. 它仅适用于内置字体系列。 It is impossible to tell from the code snippets as they stand whether you are using your TypefaceSpan or the built-in TypefaceSpan . 从代码片段站立时无法分辨它们是您使用的是TypefaceSpan还是内置的TypefaceSpan

(pro tip: use a different class name for yours than TypefaceSpan ) (专业提示:为您使用与TypefaceSpan不同的类名)

The error message in general means either Android cannot find the font file or cannot interpret it for some reason. 该错误消息通常表示Android找不到字体文件或由于某种原因无法解释它。 Given your code, your font file would need to be: 给定您的代码,您的字体文件将需要为:

assets/fonts/fonts/Raleway-Bold.ttf

in your project, as you have fonts/ both in your TypefaceSpan and in your use of that TypefaceSpan . 在你的项目,你有fonts/无论是在你的TypefaceSpan ,在你使用的是的TypefaceSpan If the font file is not there, either move it or fix up your code to reference its actual location. 如果字体文件不存在,请移动它或修正代码以引用其实际位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM