Android 如何使用XML将自定义字体从联机添加到应用程序中的文本视图?

Android 如何使用XML将自定义字体从联机添加到应用程序中的文本视图?,android,xml,textview,Android,Xml,Textview,我想在Android应用程序中添加一个名为SamsungOne的定制三星字体,我知道你可以将一个字体从网上链接到网站上,但是如何在应用程序中实现这一点,但使用XML?Java很好,但XML会更好。有人能帮忙吗?在应用程序中添加从internet下载的.ttf⟶ src⟶ 主要的⟶ 资产。 然后,您可以使用此代码段将其应用于文本视图、编辑文本等 TextView t = (TextView) findViewById(R.id.textView3); Typeface typeface = Typ

我想在Android应用程序中添加一个名为SamsungOne的定制三星字体,我知道你可以将一个字体从网上链接到网站上,但是如何在应用程序中实现这一点,但使用XML?Java很好,但XML会更好。有人能帮忙吗?

应用程序中添加从internet下载的
.ttf
⟶ src⟶ 主要的⟶ 资产
。 然后,您可以使用此代码段将其应用于文本视图、编辑文本等

TextView t = (TextView) findViewById(R.id.textView3);
Typeface typeface = Typeface.createFromAsset(getAssets(), "century_gothic.ttf"); 
                   // century_gothic.ttf is the name of your .ttf file stored in assets.
t.setTypeface(typeface);

您可以在资产文件夹中复制该字体并使用它。使用java:

typeface = Typeface.createFromAsset(context.getApplicationContext().getAssets(),
                        typefaceName);
要从xml使用它,您需要创建一个自定义视图(TextView或Edittext,无论您想要什么)。在attrs.xml中,您可以定义所有类型的字体,并使用该属性设置字体。请参见下面的编辑文本的自定义实现:-

public class CustomTextView extends TextView {

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

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

    handleStyleable(context, attrs);
}

private void handleStyleable(Context context, AttributeSet attrs) {
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomFont);
    FONT_VAL font_val= FONT_VAL.NONE;
    try {
        for (FONT_VAL mode : FONT_VAL.values()) {
            if (ta.getInt(R.styleable.CustomFont_typeface, 3) == mode.getId()) {
                font_val = mode;
                break;
            }
        }
        if (font_val == FONT_VAL.MEDIUM_FONT) {
            setTypeface(AppUtil.getTypeface(context, Constants.FontName.MEDIUM));
        }else if(font_val== FONT_VAL.REGULAR_FONT){
            setTypeface(AppUtil.getTypeface(context, Constants.FontName.REGULAR));
        }else if(font_val== FONT_VAL.LIGHT_FONT){
            setTypeface(AppUtil.getTypeface(context, Constants.FontName.LIGHT));
        }else if(font_val== FONT_VAL.ITALIC){
            setTypeface(AppUtil.getTypeface(context, Constants.FontName.ITALIC));
        }else if(font_val== FONT_VAL.BOLD){
            setTypeface(AppUtil.getTypeface(context, Constants.FontName.BOLD));
        }
    }catch (Exception e){
        e.printStackTrace();
    }
}

public enum FONT_VAL {
NONE(0),MEDIUM_FONT(1), REGULAR_FONT(2),LIGHT_FONT(3), ITALIC(4),BOLD(5);
private final int ID;
FONT_VAL(final int id) {
    this.ID = id;
}
public int getId() {
    return ID;
}
为attrs.xml中的每个字体定义自定义属性:-

<declare-styleable name="CustomFont">
    <attr name="typeface" format="enum">
        <enum name="medium_font" value="1" />
        <enum name="regular_font" value="2" />
        <enum name="light_font" value="3" />
        <enum name="italic" value="4" />
        <enum name="bold" value="5" />
    </attr>
</declare-styleable>

你不想用字体吗?
    <com.views.CustomTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        app:typeface="bold" />
Typeface typeface = Typeface.createFromFile(
new File(Environment.getExternalStorageDirectory(), "font.ttf"));