簡體   English   中英

ANDROID STUDIO 如何導入外部字體?

[英]How to import external font/ typeface in ANDROID STUDIO?

我想知道如何在 Android Studio 中使用外部字體,因為沒有Assets 文件夾 我在 inte.net 上尋找有用的教程,但他們都假裝使用 Assets 文件夾。

我在src/main中自己創建了一個資產文件夾,但 Android Studio 無法識別getAssets()

繼續你的項目:app - > src - > main

像這樣創建資產文件夾:

|assets

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

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

|java

|res

AndroidManifest.xml

然后使用

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

如果您有自定義字體,請使用以下代碼:

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

同時將您的字體文件放在assets / fonts文件夾中,並按照此處的說明進行操作

注意:您必須自己創建資產文件夾

將您的字體文件(例如 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 Level 16+(更多信息: https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml

如果您使用“getAssets()”方法出錯,則可以使用以下方法。 在assets文件夾中放置字體系列

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

如果您嘗試了res / asset / font和main / asset字體文件夾,並且您嘗試了不同的字體,但它無法正常工作 - 它很可能是Android Studio的錯誤。

我有同樣的問題,但我通過將我的字體導入在線字體編輯器(搜索pentacom字體編輯器)並導出字體並將其保存到新的ttf文件解決了它。 結果字體的分辨率會降低,但它對我有用。

您可以嘗試其他在線字體編輯/出口商。

根據Android開發人員的說法: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

要使用外部fonts,首先下載.tff格式的字體谷歌字體-Roboto

添加字體資產文件夾,如下圖所示

在此處輸入圖像描述

在此處輸入圖像描述

創建字體資源文件夾后,將下載的.tff 字體復制並粘貼到“字體”文件夾中。 (確保名稱格式正確。)

使用 android:fontFamily="@font/splashfont" 屬性引用主題中的字體。xml 或任何布局。

這就是你在 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" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM