【问题标题】:How to import google fonts in Android Studio?如何在 Android Studio 中导入谷歌字体?
【发布时间】:2018-01-17 15:53:40
【问题描述】:

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

【问题讨论】:

  • 您想要字体作为您的 App Font 还是 Android studio 字体?
  • 我主要想作为应用字体

标签: android-studio android-fonts


【解决方案1】:

这很容易。

  1. 放置一个 TextView。
  2. 在属性窗格中,选择字体系列下拉菜单。如果它不存在,找到插入符号的东西(> 并单击它以展开文本属性菜单) 展开字体系列下拉菜单。
  3. 在小列表中,一直向下滚动直到看到more fonts
  4. 这将打开一个对话框,您可以在其中从 Google 字体进行搜索
  5. 使用顶部的搜索栏搜索您喜欢的字体
  6. 选择您的字体。
  7. 选择您喜欢的字体样式(即粗体、普通、斜体等)
  8. 在右侧窗格中,选择显示将字体添加到项目的单选按钮
  9. 单击确定。现在你的 TextView 有了你喜欢的字体!

奖励:如果您想使用所选字体在应用程序中使用文本设置所有样式,只需将 <item name="android:fontfamily">@font/fontnamehere</item> 添加到您的 styles.xml 文件中即可。

【讨论】:

  • 非常感谢!我必须设置 <item name="fontFamily">@font/fontnamehere</item> 才能让它工作。
【解决方案2】:

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

1.选择File>New>Folder>Assets Folder

2.点击finish

3.右击assets并创建一个名为fonts

的文件夹

4.把你的字体文件放到assets > fonts

5.then 代码更改字体(或使用该字体创建新主题。

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

【讨论】:

    【解决方案3】:

    要使用外部字体,请先下载 .tff 格式的字体 Google font- Roboto

    如下图所示添加字体资源文件夹

    创建字体资源文件夹后,将下载的 .tff 字体复制并粘贴到“字体”文件夹中。 (确保名称格式正确。)

    在您的 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>
    

    这就是你在文本视图中的做法

        <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" />
    

    【讨论】:

      【解决方案4】:

      如果您想为您的 android studio ide 使用这些字体,请下载并安装字体并更改 android studio 代码文本字体。

      如果您想为创建的 android 应用程序使用字体,则必须将所有 .ttf 文件放在应用程序的资产文件夹中。请谷歌如何创建资产文件夹以及如何在您创建的应用程序中使用自定义字体

      【讨论】:

        猜你喜欢
        • 2018-03-12
        • 2015-08-30
        • 2015-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-11
        • 2015-08-17
        • 2015-04-08
        相关资源
        最近更新 更多