所以我想在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”是一个愚蠢的尝试!


当前回答

也许我的评论会对某些人有用:我试图了解为什么app:fontFamily=“@font/my_font”不起作用,在浪费了一段时间后发现,有效的解决方案是android:fontFamily=“@font/my_font”

其他回答

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

在XML中,您可以做的是

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

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

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

Kotlin代码-从资源文件夹设置自定义字体的文本视图

从res->font->avenir_ext_regular.ttf设置自定义字体

textView!!.typeface = ResourcesCompat.getFont(context!!, R.font.avenir_next_regular)

在这里,您可以看到所有可用的字体系列值及其对应的字体文件名(该文件在android 5.0+中使用)。在移动设备上,您可以在以下位置找到它:

/system/etc/fonts.xml (for 5.0+)

(对于使用此版本的android 4.4及以下版本,但我认为fonts.xml格式更清晰,易于理解。)

例如

    <!-- first font is default -->
20    <family name="sans-serif">
21        <font weight="100" style="normal">Roboto-Thin.ttf</font>
22        <font weight="100" style="italic">Roboto-ThinItalic.ttf</font>
23        <font weight="300" style="normal">Roboto-Light.ttf</font>
24        <font weight="300" style="italic">Roboto-LightItalic.ttf</font>
25        <font weight="400" style="normal">Roboto-Regular.ttf</font>
26        <font weight="400" style="italic">Roboto-Italic.ttf</font>
27        <font weight="500" style="normal">Roboto-Medium.ttf</font>
28        <font weight="500" style="italic">Roboto-MediumItalic.ttf</font>
29        <font weight="900" style="normal">Roboto-Black.ttf</font>
30        <font weight="900" style="italic">Roboto-BlackItalic.ttf</font>
31        <font weight="700" style="normal">Roboto-Bold.ttf</font>
32        <font weight="700" style="italic">Roboto-BoldItalic.ttf</font>
33    </family>

family标记的name属性name=“sans-serif”定义了可以在android中使用的值:fontFamily。

字体标记定义对应的字体文件。

在这种情况下,可以忽略<!--下的源代码回退字体-->,用于字体的回退逻辑。

我使用Letter Press库来处理我的非文本视图内容,如Buttons和kianoni fontloader库处理我的文本视图,因为在这个库中使用样式比Letter Press更容易,我得到了理想的反馈。这对于那些想要使用除Roboto字体之外的自定义字体的人来说非常有用。这是我使用字体库的经验。对于那些希望使用自定义类更改字体的人,我强烈建议使用以下代码段创建此类

public class TypefaceSpan extends MetricAffectingSpan {
    /** An <code>LruCache</code> for previously loaded typefaces. */
    private static LruCache<String, Typeface> sTypefaceCache =
            new LruCache<String, Typeface>(12);

    private Typeface mTypeface;

    /**
     * Load the {@link android.graphics.Typeface} and apply to a {@link android.text.Spannable}.
     */
    public TypefaceSpan(Context context, String typefaceName) {
        mTypeface = sTypefaceCache.get(typefaceName);

        if (mTypeface == null) {
            mTypeface = Typeface.createFromAsset(context.getApplicationContext()
                    .getAssets(), String.format("fonts/%s", typefaceName));

            // Cache the loaded Typeface
            sTypefaceCache.put(typefaceName, mTypeface);
        }
    }

    @Override
    public void updateMeasureState(TextPaint p) {
        p.setTypeface(mTypeface);

        // Note: This flag is required for proper typeface rendering
        p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }

    @Override
    public void updateDrawState(TextPaint tp) {
        tp.setTypeface(mTypeface);

        // Note: This flag is required for proper typeface rendering
        tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }
} 

并像这样使用类:

AppData = PreferenceManager.getDefaultSharedPreferences(this);
TextView bannertv= (TextView) findViewById(R.id.txtBanner);
    SpannableString s = new SpannableString(getResources().getString(R.string.enterkey));
    s.setSpan(new TypefaceSpan(this, AppData.getString("font-Bold",null)), 0, s.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    bannertv.setText(s);

也许这有帮助。

它与android相同:字体。

内置字体有:

典型的桑斯衬线单空间

参见android:字体。