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

时间:2018-06-01 07:29:21

标签: android android-studio fonts android-xml android-fonts

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

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

2 个答案:

答案 0 :(得分:0)

正如Jay Thummar建议的那样,我重命名了我的字体,没有空格,只有小写字母,并且它有效。

答案 1 :(得分:0)

  

在项目中包含以下课程

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();
        }
    }
}
  

将属性添加到R​​es&gt;值&GT; Attrs文件

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

创建一个要更改字体的小部件:假设 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文件代码:

<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" />
  

注意:您想要的字体必须位于资源&gt;如果您复制完整代码,则为fonts文件夹。

     

为所需的字体名称定义一个字符串,如:

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