如何更改 XML 文件 android studio 中的字体

How to change font in XML file android studio

我创建了一个字体文件夹,添加了我的字体 (Tilitium Bold) 以及如何更改按钮的字体。

我试过 android:fontFamily="@font/Titilum-Bold" 但它不起作用。有任何想法吗?

正如 Jay Thummar 所建议的,我重命名了我的字体,没有空格,只有小写字母,并且成功了。

Include Following Class to your Project

public class FontManager {

    private static FontManager sInstance;
    private HashMap<String, Typeface> mFontCache = new HashMap<>(); // <path to the font file in the assets folder, cached Typeface>

    /**
     * Gets the FontManager singleton
     *
     * @return the FontManager singleton
     */
    public static FontManager getInstance() {
        if (sInstance == null) {
            sInstance = new FontManager();
        }
        return sInstance;
    }

    /**
     * Gets a Typeface from the cache. If the Typeface does not exist, creates it, cache it and returns it.
     *
     * @param context a Context
     * @param path    Path to the font file in the assets folder. ie "fonts/MyCustomFont.ttf"
     * @return the corresponding Typeface (font)
     * @throws RuntimeException if the font asset is not found
     */
    public Typeface getTypeface(Context context, String path) throws RuntimeException {
        Typeface typeface = mFontCache.get(path);
        if (typeface == null) {
            try {
                typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" +path);
            } catch (RuntimeException exception) {
                String message = "Font assets/" + path + " cannot be loaded";
                throw new RuntimeException(message);
            }
            mFontCache.put(path, typeface);
        }
        return typeface;
    }

    /**
     * Gets a Typeface from the cache. If the Typeface does not exist, creates it, cache it and returns it.
     *
     * @param context   a Context
     * @param pathResId String resource id for the path to the font file in the assets folder. ie "fonts/MyCustomFont.ttf"
     * @return the corresponding Typeface (font)
     * @throws RuntimeException if the resource or the font asset is not found
     */
    public Typeface getTypeface(Context context, int pathResId) throws RuntimeException {
        try {
            String path = context.getResources().getString(pathResId);
            return getTypeface(context, path);
        } catch (Resources.NotFoundException exception) {
            String message = "String resource id " + pathResId + " not found";
            throw new RuntimeException(message);
        }
    }

    /**
     * Applies a font to a TextView that uses the "fontPath" attribute.
     *
     * @param textView TextView when the font should apply
     * @param attrs    Attributes that contain the "fontPath" attribute with the path to the font file in the assets folder
     */
    public void applyFont(TextView textView, AttributeSet attrs) {
        if (attrs != null) {
            Context context = textView.getContext();
            TypedArray styledAttributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.customTextView, 0, 0);
            String fontPath = styledAttributes.getString(R.styleable.customTextView_fontFamily);
            if (!TextUtils.isEmpty(fontPath)) {
                Typeface typeface = getTypeface(context, fontPath);
                if (typeface != null) {
                    textView.setTypeface(typeface);
                }
            }
            styledAttributes.recycle();
        }
    }
}

Add Attributes to Res> Values> Attrs file

 <declare-styleable name="customTextView">
        <attr name="fontFamily" format="string" />
 </declare-styleable>

Create a Widget for which you want to change fonts:Let's say CustomTextView

public class CustomTextView extends AppCompatButton {

        public CustomTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            applyDefaultFont(context);
            FontManager.getInstance().applyFont(this, attrs);
        }

        private void applyDefaultFont(Context context) {
            Typeface typeface = FontManager.getInstance().getTypeface(context, context.getString(R.string.font_regular));
            if (typeface != null) {
                this.setTypeface(typeface);
            }
        }

    }

XML file Code:

<com.yourpackage.CustomButton
                android:id="@+id/btnSubmit"
                android:layout_width="match_parent"
                android:layout_height="?actionBarSize"
                android:layout_marginTop="@dimen/_16sdp"
                android:background="@drawable/shape_button_green"
                android:text="@string/submit"
                android:textAllCaps="true"
                android:textColor="@color/white"
                android:textSize="@dimen/font_button"
                app:fontFamily="@string/font_bold" />

Note: Your desired font must be in assets > fonts folder if you copy the complete code.

Define a string for desired font name like:

<string name="font_regular">SF-Pro-Text-Regular.otf</string>
<string name="font_bold">SF-Pro-Text-Bold.otf</string>