如何在Android Studio中导入Google字体?

9

我完全是Android Studio的新手,我想知道如何导入新字体(更准确地说:Roboto和Pacifico字体系列)


你想要作为 App 字体或 Android Studio 字体的字体? - meditat
我主要想要作为应用程序字体。 - HUHO
4个回答

13

很简单。

  1. 放置一个TextView。
  2. 在属性窗格中,选择字体家族下拉列表。如果没有,请找到插入符号(>)并单击它以展开文本属性菜单。
  3. 在小列表中,向下滚动直到看到 更多字体
  4. 这将打开一个对话框,在其中您可以从Google Fonts搜索。
  5. 使用顶部的搜索栏搜索您喜欢的字体。
  6. 选择您的字体。
  7. 选择您喜欢的字体风格(例如粗体、正常、斜体等)。
  8. 在右侧窗格中,选择名为 Add font to project 的单选按钮
  9. 点击确定。现在,您的TextView拥有您喜欢的字体!

BONUS: 如果您想要使用所选字体样式所有应用程序中的所有文本,请将 <item name="android:fontfamily">@font/fontnamehere</item> 添加到您的 styles.xml 文件。


1
太好了,谢谢!我必须设置 <item name="fontFamily">@font/fontnamehere</item> 才能让它工作。 - Laurence Cooper
最短且最聪明的解决方案!谢谢!! 此外,您可以在此应用之前观察任何字体: https://fonts.google.com/?preview.text=This%20is%20a%20story&preview.text_type=custom - Araz
如果我在Google字体(网页)中找到了字体,但AS中的搜索无法找到它,那该怎么办? - ProjectDelta

5

要设置步骤的字体,有一些简单的步骤。

1.选择 文件>新建>文件夹>资产文件夹

enter image description here

2. 点击 finish

3. 右键点击 assets,创建一个名为fonts的文件夹

4. 将你的字体文件放入 assets > fonts

5. 接下来编写代码以更改您的字体 (或使用该字体创建新的主题。) enter image description here

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/anyFont.ttf");
//and then use the typeface for changing the font using `textView.setTypeface(tf)`

5
为了使用外部字体,首先要下载.tff格式的字体,比如Google字体-Roboto
按照下面图片所示的方式,添加字体资源文件夹。 enter image description here enter image description here 创建好字体资源文件夹后,将下载的.ttf字体复制并粘贴到"font"文件夹中(确保名称格式正确)。
在theme.xml或任何布局中引用字体,使用android:fontFamily="@font/splashfont"属性。以下是在theme.xml文件中实现的方法。
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.FishPott" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/color_black_level_1</item>
    <item name="colorPrimaryVariant">@color/color_black_level_2</item>
    <item name="colorOnPrimary">@color/white</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/color_black_level_1</item>
    <item name="colorSecondaryVariant">@color/color_black_level_2</item>
    <item name="colorOnSecondary">@color/color_white_level_1</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
    <item name="android:fontFamily">@font/robotoregular</item>
</style>

这是在TextView中实现的方法

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/activity_start_fp_MaterialTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="32dp"
        android:fontFamily="@font/splashfont"
        android:gravity="center"
        android:text="MyText"
        android:textColor="@color/color_black_level_1"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/activity_start_logo_ShapeableImageView" />

-3
如果您想在Android Studio IDE中使用这些字体,请下载并安装字体,然后更改Android Studio代码文本字体。
如果您想为创建的Android应用程序使用字体,则必须将所有.ttf文件放置在应用程序的资产文件夹中。请搜索如何创建资产文件夹以及如何在创建的应用程序中使用自定义字体。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接