【问题标题】:How to import external font/ typeface in ANDROID STUDIO?如何在 ANDROID STUDIO 中导入外部字体/字体?
【发布时间】:2021-08-19 19:43:21
【问题描述】:

我想知道如何在 Android Studio 中使用外部字体,因为没有 Assets 文件夹。我在互联网上寻找有用的教程,但他们都假装使用 Assets 文件夹。

我自己在src/main 中创建了一个资产文件夹,但Android Studio 无法识别getAssets()

【问题讨论】:

标签: android fonts android-studio


【解决方案1】:

继续你的项目:app-->src-->main

像这样创建一个资产文件夹:

|assets

    |-----------------fonts

        |-------------------font.ttf

|java

|res

AndroidManifest.xml

然后使用

     Typeface face=Typeface.createFromAsset(getAssets(),"fonts/digital.ttf");
     txtV.setTypeface(face);

【讨论】:

    【解决方案2】:

    如果您有自定义字体,请使用以下代码:

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

    还将您的字体文件放在 assets/fonts 文件夹中,然后从这里关注instructions

    注意:你必须自己制作资产文件夹

    【讨论】:

      【解决方案3】:

      将您的字体文件(例如 customfont.tff 和 customfont_bold.tff)放在文件夹 app>src>res>font> 下(注意:如果它不存在,请创建字体文件夹)

      另外,在同一文件夹内创建一个名为 fonts.xml 的文件,内容如下:

        <font-family xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto">
      
              <font
                      app:fontStyle="normal"
                      app:fontWeight="700"
                      app:font="@font/customfont"/>
      
              <font
                      app:fontStyle="normal"
                      app:fontWeight="700"
                      app:font="@font/customfont_bold"/>
        </font-family>
      

      然后,编辑文件 app>src>res>values>styles.xml 为整个应用应用默认字体。

      <!-- Base application theme. -->
      <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
          <item name="colorPrimary">@color/colorPrimary</item>
          <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
          <item name="colorAccent">@color/colorAccent</item>
          <item name="android:fontFamily">@font/customfont</item>
      </style>
      

      如果您想更改单个 UI 元素的字体:

      <TextView
          android:fontFamily="@font/customfont"
          android:layout_height="wrap_content"
          android:layout_width="wrap_content"
          android:textColor="@color/black"
          android:textSize="18sp"
          android:text="Some text"
          />
      

      注意:此方法适用于 API 级别 16+(更多信息:https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml

      【讨论】:

        【解决方案4】:

        要使用外部字体,请先下载 .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" />
        

        【讨论】:

          【解决方案5】:

          如果您在使用“getAssets()”方法时遇到错误,那么您可以使用以下方法。 在资产文件夹中放置字体系列

          Typeface getFace=Typeface.create("OpenSans",3);
          textView =  (TexView) findViewById(R.id.textView);
          textView.setTypeface(getFace);
          

          【讨论】:

            【解决方案6】:

            如果您同时尝试了 res/asset/font 和 main/asset 字体文件夹,并且尝试了不同的字体,但都失败了——这很可能是 Android Studio 错误。

            我遇到了同样的问题,但我通过将我的字体导入在线字体编辑器(搜索 pentacom 字体编辑器)并导出字体并将其保存到新的 ttf 文件来解决它。生成的字体分辨率会较低,但对我有用。

            您可能还可以尝试其他在线字体编辑器/导出器。

            【讨论】:

              【解决方案7】:

              根据安卓开发者的说法: Android 8.0(API 级别 26)引入了一项新功能,即 XML 中的字体,它允许您将字体用作资源。您可以在 res/font/ 文件夹中添加字体文件以将字体捆绑为资源。这些字体在您的 R 文件中编译,并在 Android Studio 中自动可用。您可以借助新的资源类型字体来访问字体资源。例如,要访问字体资源,请使用 @font/myfont 或 R.font.myfont。

              更多详情:https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html

              【讨论】:

                猜你喜欢
                • 2018-03-12
                • 1970-01-01
                • 2010-11-28
                • 2012-02-24
                • 2013-12-18
                • 2012-01-11
                • 2012-07-29
                相关资源
                最近更新 更多