如何在XML中使用fontfamily设置自定义字体。 Api 16

时间:2017-05-04 10:23:26

标签: android fonts

我目前正在使用android studio 2.3.1。

我希望能够从xml而不是以编程方式设置textview上的字体。

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

那么如何从我的资源文件夹访问我的字体? 我看到Android O将带有一个名为" font"。

的资源文件夹

1 个答案:

答案 0 :(得分:0)

创建TypeFactory.java类,如下所示。

public class TypeFactory {

    private String A_BOLD= "Amble-Bold.ttf";
    private String A_LIGHT="Amble-Light.ttf";
    private String A_REGULAR= "Amble-Regular.ttf";
    private String O_ITALIC= "OpenSans-Italic.ttf";
    private String O_REGULAR="OpenSans-Regular.ttf";

    Typeface ambleBold;
    Typeface ambleLight;
    Typeface ambleRegular;
    Typeface openSansItalic;
    Typeface openSansRegular;

    public TypeFactory(Context context){
        ambleBold = Typeface.createFromAsset(context.getAssets(),A_BOLD);
        ambleLight = Typeface.createFromAsset(context.getAssets(),A_LIGHT);
        ambleRegular = Typeface.createFromAsset(context.getAssets(),A_REGULAR);
        openSansItalic = Typeface.createFromAsset(context.getAssets(),O_ITALIC);
        openSansRegular = Typeface.createFromAsset(context.getAssets(),O_REGULAR);
    }

}

下面给出了CustomTextView.java类。

public class CustomTextView extends TextView {

    private int typefaceType;
    private TypeFactory mFontFactory;

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        applyCustomFont(context, attrs);
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        applyCustomFont(context, attrs);
    }

    public CustomTextView(Context context) {
        super(context);
    }

    private void applyCustomFont(Context context, AttributeSet attrs) {


        TypedArray array = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.CustomTextView,
                0, 0);
        try {
            typefaceType = array.getInteger(R.styleable.CustomTextView_font_name, 0);
        } finally {
            array.recycle();
        }
        if (!isInEditMode()) {
            setTypeface(getTypeFace(typefaceType));
        }

    }

    public Typeface getTypeFace(int type) {
        if (mFontFactory == null)
            mFontFactory = new TypeFactory(getContext());

        switch (type) {
            case Constants.A_BOLD:
                return mFontFactory.ambleBold;

            case Constants.A_LIGHT:
                return mFontFactory.ambleLight;

            case Constants.A_REGULAR:
                return mFontFactory.ambleRegular;

            case Constants.O_LIGHT:
                return mFontFactory.openSansItalic;

            case Constants.O_REGULAR:
                return mFontFactory.openSansRegular;

            default:
                return mFontFactory.ambleBold;
        }
    }

    public interface Constants {
        int A_BOLD = 1,
                A_LIGHT = 2,
                A_REGULAR = 3,
                O_LIGHT = 4,
        O_REGULAR=5;
    }


}

R.styleable.CustomTextView在attrs.xml文件中定义,如下所示。

 <?xml version="1.0" encoding="utf-8"?>
    <resources>

        <declare-styleable name="CustomTextView">
            <attr name="font_name">
                <enum value="1" name="ambleBold"/>
                <enum value="2" name="ambleLight"/>
                <enum value="3" name="ambleRegular"/>
                <enum value="4" name="openSansItalic"/>
                <enum value="5" name="openSansRegular"/>
            </attr>
        </declare-styleable>

    </resources>

Our updated activity_custom_fonts.xml layout is given below

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.journaldev.customfonts.MainActivity">

    <com.journaldev.customfonts.CustomTextView
        app:font_name="ambleBold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Amble Bold"
        android:textSize="28sp"
        android:id="@+id/ambleBold"
         />

    <com.journaldev.customfonts.CustomTextView
        app:font_name="ambleLight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Amble Light"
        android:textSize="28sp"
        android:id="@+id/ambleLight"
        />

    <com.journaldev.customfonts.CustomTextView
        app:font_name="ambleRegular"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Amble Regular"
        android:textSize="28sp"
        android:id="@+id/ambleRegular" />

    <com.journaldev.customfonts.CustomTextView
        app:font_name="openSansRegular"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OpenSans Regular"
        android:textSize="28sp"
        android:id="@+id/opRegular" />

    <com.journaldev.customfonts.CustomTextView
        app:font_name="openSansItalic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OpenSans-Italic"
        android:id="@+id/opItalic"
        android:textSize="28sp"
        />

    <Button android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="Pacifico"
        android:id="@+id/pacifico"/>

</LinearLayout>

你也可以使用书法:https://github.com/chrisjenx/Calligraphy