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


当前回答

对于android studio 3及以上版本,您可以使用此样式,然后在应用程序中查看所有文本字体更改。

在style.xml中创建此样式:

<!--OverRide all textView font-->
<style name="defaultTextViewStyle" parent="android:Widget.TextView">
        <item name="android:fontFamily">@font/your_custom_font</item>
</style>

然后在主题中使用它:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:textViewStyle">@style/defaultTextViewStyle</item>
</style>

其他回答

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。

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

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

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

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

这里有一个很好的图书馆

    implementation 'uk.co.chrisjenx:calligraphy:2.3.0'

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

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

在SANS_SERIF中,可以使用:

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

您可以在ITALIC中使用:

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

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

您也可以在res目录下添加字体文件夹,如下所示。

然后,选择字体作为资源类型。

您可以从https://www.1001fonts.com/,然后将TTF文件提取到此字体目录。

最后,只需通过添加android:fontFamily:“@font/urfontfilename”来更改包含文本视图的XML文件