我试图在Android Studio中使用自定义字体,就像我们在Eclipse中所做的那样。但不幸的是,不知道在哪里放“资产”文件夹!


当前回答

Android 8.0 (API 26)引入了与字体相关的新功能。

1)字体可以作为资源使用。

2)可下载的字体。

如果你想在android应用程序中使用外部字体,你可以在apk中包含字体文件或配置可下载的字体。

在APK中包含字体文件:您可以下载字体文件,保存在res/字体文件器中,定义字体族,并在样式中使用字体族。

有关使用自定义字体作为资源的更多详细信息,请参阅http://www.zoftino.com/android-using-custom-fonts

配置可下载字体:通过提供字体提供程序详细信息来定义字体,添加字体提供程序证书并在样式中使用字体。

有关可下载字体的详细信息,请参阅http://www.zoftino.com/downloading-fonts-android

其他回答

把字体放在资产文件夹 然后应用fontfamily: " your fonts

首先创建资产文件夹,然后在其中创建字体文件夹。

然后你可以设置字体从资产或目录如下:

public class FontSampler extends Activity {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        TextView tv = (TextView) findViewById(R.id.custom);
        Typeface face = Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");

        tv.setTypeface(face);

        File font = new File(Environment.getExternalStorageDirectory(), "MgOpenCosmeticaBold.ttf");

        if (font.exists()) {
            tv = (TextView) findViewById(R.id.file);
            face = Typeface.createFromFile(font);

            tv.setTypeface(face);
        } else {
            findViewById(R.id.filerow).setVisibility(View.GONE);
        }
    }
} 

2021年更新:

在res文件夹中创建一个名为font的文件夹并复制您的字体

所有字体名称只能为:小写字母a-z、0-9或下划线。

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="@font/abc_font" />

编程使用:

textView.setTypeface(ResourcesCompat.getFont(context, R.font.abc_font))

对于Android Studio 4.2+,现在甚至有一个菜单选项:

Android 8.0 (API 26)引入了与字体相关的新功能。

1)字体可以作为资源使用。

2)可下载的字体。

如果你想在android应用程序中使用外部字体,你可以在apk中包含字体文件或配置可下载的字体。

在APK中包含字体文件:您可以下载字体文件,保存在res/字体文件器中,定义字体族,并在样式中使用字体族。

有关使用自定义字体作为资源的更多详细信息,请参阅http://www.zoftino.com/android-using-custom-fonts

配置可下载字体:通过提供字体提供程序详细信息来定义字体,添加字体提供程序证书并在样式中使用字体。

有关可下载字体的详细信息,请参阅http://www.zoftino.com/downloading-fonts-android

要在运行Android 4.1 (API级别16)及更高版本的设备上使用XML字体功能,请使用支持库26。有关使用支持库的更多信息,请参阅使用支持库一节。

添加字体作为资源,在Android Studio中执行以下步骤:

右键单击res文件夹,进入New > Android资源目录。 出现“新建资源目录”窗口。 在“资源类型”列表中选择字体,然后单击“确定”。 在字体文件夹中添加您的字体文件。

创建字体族 要创建字体族,请在Android Studio中执行以下步骤:

右键单击字体文件夹,选择新建>字体资源文件。出现“新建资源文件”窗口。 输入文件名,然后单击OK。新的字体资源XML在编辑器中打开。 在元素中包含每个字体文件、样式和权重属性。下面的XML演示了如何在字体资源XML中添加与字体相关的属性: 如果你的minSdkVersion是API级别26或更高

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/lobster_regular" />
    <font
        android:fontStyle="italic"
        android:fontWeight="400"
        android:font="@font/lobster_italic" />
</font-family>

如果你的minSdkVersion低于API级别26

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
    <font
        app:font="@font/lobster_italic"
        app:fontStyle="normal"
        app:fontWeight="400" />
</font-family>

然后你可以像这样在任何地方使用它

android:fontFamily="@font/your_custom_font_file"