所以我想在android中更改android:fontFamily,但我在android中没有看到任何预定义的字体。如何选择预定义的选项之一?我真的不需要定义我自己的TypeFace,但我所需要的是与现在显示的不同。

<TextView
    android:id="@+id/HeaderText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="52dp"
    android:gravity="center"
    android:text="CallerBlocker"
    android:textSize="40dp"
    android:fontFamily="Arial"
 />

看来我在上面做的事真的行不通!BTW android:fontFamily=“Arial”是一个愚蠢的尝试!


当前回答

这是以编程方式设置字体的方法:

TextView tv = (TextView) findViewById(R.id.appname);
Typeface face = Typeface.createFromAsset(getAssets(),
            "fonts/epimodem.ttf");
tv.setTypeface(face);

将字体文件放在assets文件夹中。在我的例子中,我创建了一个名为字体的子目录。

编辑:如果你想知道你的资产文件夹在哪里,请看这个问题

其他回答

要以编程方式设置Roboto:

paint.setTypeface(Typeface.create("sans-serif-thin", Typeface.NORMAL));

以编程方式将字体添加到TextView的最简单方法是,首先,将字体文件添加到项目中的“资产”文件夹中。例如,字体路径如下所示:assets/fonts/my_font.otf

并将其添加到TextView中:

科特林

val font_path = "fonts/my_font.otf"  

myTypeface = Typeface.createFromAsset(MyApplication.getInstance().assets, font_path)

textView.typeface = myTypeface

Java

String font_path = "fonts/my_font.otf";
Typeface myTypeface = Typeface.createFromAsset(MyApplication.getInstance().assets, font_path)
textView.setTypeface(myTypeface);
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="UnusedAttribute">
    <font
        android:font="@font/google_sans_regular"
        android:fontStyle="normal"
        android:fontWeight="400"
        app:font="@font/google_sans_regular"
        app:fontStyle="normal"
        app:fontWeight="400" />

    <font
        android:font="@font/google_sans_italic"
        android:fontStyle="italic"
        android:fontWeight="400"
        app:font="@font/google_sans_italic"
        app:fontStyle="italic"
        app:fontWeight="400" />

    <font
        android:font="@font/google_sans_medium"
        android:fontStyle="normal"
        android:fontWeight="500"
        app:font="@font/google_sans_medium"
        app:fontStyle="normal"
        app:fontWeight="500" />

    <font
        android:font="@font/google_sans_medium_italic"
        android:fontStyle="italic"
        android:fontWeight="500"
        app:font="@font/google_sans_medium_italic"
        app:fontStyle="italic"
        app:fontWeight="500" />

    <font
        android:font="@font/google_sans_bold"
        android:fontStyle="normal"
        android:fontWeight="600"
        app:font="@font/google_sans_bold"
        app:fontStyle="normal"
        app:fontWeight="600" />

    <font
        android:font="@font/google_sans_bold_italic"
        android:fontStyle="italic"
        android:fontWeight="600"
        app:font="@font/google_sans_bold_italic"
        app:fontStyle="italic"
        app:fontWeight="600" />

</font-family>

android:fontFamily的有效值在/system/etc/system_fonts.xml(4.x)或/system/etc/fonts.xml(5.x)中定义。但设备制造商可能会对其进行修改,因此设置fontFamily值所使用的实际字体取决于指定设备的上述文件。

在AOSP中,Arial字体是有效的,但必须使用“Arial”而不是“Arial”来定义,例如android:fontFamily=“Arial“。仔细看看Kitkat的system_fonts.xml

    <family>
    <nameset>
        <name>sans-serif</name>
        <name>arial</name>
        <name>helvetica</name>
        <name>tahoma</name>
        <name>verdana</name>
    </nameset>
    <fileset>
        <file>Roboto-Regular.ttf</file>
        <file>Roboto-Bold.ttf</file>
        <file>Roboto-Italic.ttf</file>
        <file>Roboto-BoldItalic.ttf</file>
    </fileset>
</family>

//////////////////////////////////////////////////////////////////////////

在布局中定义“字体”有三个相关的xml属性——android:fontFamily、android:type和android:textStyle。“fontFamily”和“textStyle”或“字体”和“文本样式”的组合可用于更改文本中字体的外观,单独使用也是如此。TextView.java中的代码段如下:

    private void setTypefaceFromAttrs(String familyName, int typefaceIndex, int styleIndex) {
    Typeface tf = null;
    if (familyName != null) {
        tf = Typeface.create(familyName, styleIndex);
        if (tf != null) {
            setTypeface(tf);
            return;
        }
    }
    switch (typefaceIndex) {
        case SANS:
            tf = Typeface.SANS_SERIF;
            break;

        case SERIF:
            tf = Typeface.SERIF;
            break;

        case MONOSPACE:
            tf = Typeface.MONOSPACE;
            break;
    }
    setTypeface(tf, styleIndex);
}


    public void setTypeface(Typeface tf, int style) {
    if (style > 0) {
        if (tf == null) {
            tf = Typeface.defaultFromStyle(style);
        } else {
            tf = Typeface.create(tf, style);
        }

        setTypeface(tf);
        // now compute what (if any) algorithmic styling is needed
        int typefaceStyle = tf != null ? tf.getStyle() : 0;
        int need = style & ~typefaceStyle;
        mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0);
        mTextPaint.setTextSkewX((need & Typeface.ITALIC) != 0 ? -0.25f : 0);
    } else {
        mTextPaint.setFakeBoldText(false);
        mTextPaint.setTextSkewX(0);
        setTypeface(tf);
    }
}

从代码中我们可以看到:

如果设置了“fontFamily”,则将忽略“字体”。“字体”具有标准和有限的有效值。事实上,这些值是“normal”、“sans”、“serif”和“monospace”,它们可以在system_fonts.xml(4.x)或fonts.xml(5.x)中找到。实际上,“normal“和”sans“都是系统的默认字体。“fontFamily”可用于设置内置字体的所有字体,而“字体”仅提供“sans-serif”、“serif”和“monospace”(世界上三大字体类型)的典型字体。当只设置“textStyle”时,我们实际上设置了默认字体和指定的样式。有效值为“normal”、“bold”、“italic”和“bold | italic”。

你想要的是不可能的。您必须在代码中设置TypeFace。

在XML中,您可以做的是

android:typeface="sans" | "serif" | "monospace"

除此之外,您无法使用XML中的字体。:)

对于Arial,您需要在代码中设置字体。