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


当前回答

您可以使用以下库轻松完成

https://github.com/sunnag7/FontStyler

<com.sunnag.fontstyler.FontStylerView
              android:textStyle="bold"
              android:text="@string/about_us"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:paddingTop="8dp"
              app:fontName="Lato-Bold"
              android:textSize="18sp"
              android:id="@+id/textView64" />

它重量轻,易于实现,只需在asset文件夹中复制字体并在xml中使用名称即可。

其他回答

如果你想编程,你可以使用

label.setTypeface(Typeface.SANS_SERIF, Typeface.ITALIC);

在SANS_SERIF中,可以使用:

违约默认_文件夹单空间桑塞里夫衬线

您可以在ITALIC中使用:

粗体粗体_斜体斜体正常的,正常的

所有这些都是在Android开发者上声明的

Android不允许您从XML布局设置自定义字体。相反,您必须将特定字体文件绑定到应用程序的assets文件夹中,并以编程方式进行设置。类似于:

TextView textView = (TextView) findViewById(<your TextView ID>);
Typeface typeFace = Typeface.createFromAsset(getAssets(), "<file name>");
textView.setTypeface(typeFace);

注意,只有在调用setContentView()后才能运行此代码。此外,Android只支持某些字体,并且应为.ttf(TrueType)或.otf(OpenType)格式。即使如此,某些字体也可能不起作用。

这是一种在Android上绝对有效的字体,如果Android不支持您的字体文件,您可以使用它来确认您的代码是否有效。

Android O更新:根据Roger的评论,这现在可以在Android O中使用XML。

<?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>

经过反复尝试,我学到了以下内容。

在*.xml中,您可以将常用字体与以下功能结合起来,而不仅仅是字体:

 android:fontFamily="serif" 
 android:textStyle="italic"

有了这两种样式,在任何其他情况下都不需要使用字体。fontfamily&textStyle的组合范围更大。

如果您想在许多地方使用具有相同字体系列的TextView,请扩展TextView类并按如下方式设置字体:-

public class ProximaNovaTextView extends TextView {
    public ProximaNovaTextView(Context context) {
        super(context);
        applyCustomFont(context);
    }
    public ProximaNovaTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        applyCustomFont(context);
    }
    public ProximaNovaTextView(Context context, AttributeSet attrs, int defStyle) {
       super(context, attrs, defStyle);
       applyCustomFont(context);
    } 
    private void applyCustomFont(Context context) {
        Typeface customFont = FontCache.getTypeface("proximanova_regular.otf", context);
        setTypeface(customFont);
    }
}

然后对TextView使用XML中的这个自定义类,如下所示:-

<com.myapp.customview.ProximaNovaTextView
android:id="@+id/feed_list_item_name_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
/>