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


当前回答

在项目中添加字体

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

1 -右键单击res文件夹,进入New > Android资源目录。 出现“新建资源目录”窗口。

2 -在“资源类型”列表中,选择“字体”,单击“确定”。 3 -添加您的字体文件在字体文件夹只是一个简单的复制和粘贴。注意字体的名称应该是小写的。

使用XML布局中的字体

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

向样式添加字体

<style name="customfontstyle" parent="@android:style/TextAppearance.Small">
<item name="android:fontFamily">@font/lobster</item>
</style>

以编程方式使用字体

科特林:

val typeface = resources.getFont(R.font.myfont)
textView.typeface = typeface

JAVA:

Typeface typeface = getResources().getFont(R.font.myfont);
textView.setTypeface(typeface);

其他回答

我认为我们可以使用谷歌字体而不是下载。ttf文件。这很容易实现。你只需要遵循这些步骤。 步骤1)打开你的项目的layout.xml和属性中的文本视图的选择字体家族(作为参考截图附在后面)

步骤2)in字体家族选择更多字体..选项,如果你的字体不在那里。然后你会看到一个新的窗口将打开,在那里你可以输入你需要的字体,并从列表中选择所需的字体,即常规,粗体,斜体等。如下图所示。

步骤3)然后你会看到一个字体文件夹将自动生成在/res文件夹有你选择的字体xml文件。

然后你可以直接在xml中使用这个字体家族作为

      android:fontFamily="@font/josefin_sans_bold"

或者从专业语法上讲,你可以使用

  Typeface typeface = ResourcesCompat.getFont(this, R.font.app_font);
  fontText.setTypeface(typeface);

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- o和Android Studio 2.4的回答

在res文件夹下创建名为font的文件夹。下载要添加到项目示例谷歌fonts中的各种字体 在xml用户字体家族中 例子: < TextView android: fontFamily = " @font / indie_flower” android: layout_width = " wrap_content " android: layout_height = " wrap_content " android:填充= " 10 dp” android:文本= " @string / sample_text " / >

3.如果您希望它以编程的方式使用下面的代码

Typeface typeface = getResources().getFont(R.font.indie_flower);
textView.setTypeface(typeface);

要了解更多信息,请链接到我的博客文章字体样式的Android与Android Studio 2.4

在Project -> app(或您的app名称)-> src -> main ->右键单击->新建->目录中创建文件夹资产。 然后在资产中创建一个名为fonts的新目录。

将字体分配给textView:

TextView textView = (TextView) findViewById(R.id.your_textView);

final Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/your_font_name");

Your_font_name包含字体扩展名。

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