【发布时间】:2018-01-17 15:53:40
【问题描述】:
我是 Android Studio 的初学者,我想知道如何导入新字体(更准确地说:Roboto 和 Pacifico 字体系列)
【问题讨论】:
-
您想要字体作为您的 App Font 还是 Android studio 字体?
-
我主要想作为应用字体
标签: android-studio android-fonts
我是 Android Studio 的初学者,我想知道如何导入新字体(更准确地说:Roboto 和 Pacifico 字体系列)
【问题讨论】:
标签: android-studio android-fonts
这很容易。
more fonts
奖励:如果您想使用所选字体在应用程序中使用文本设置所有样式,只需将 <item name="android:fontfamily">@font/fontnamehere</item> 添加到您的 styles.xml 文件中即可。
【讨论】:
<item name="fontFamily">@font/fontnamehere</item> 才能让它工作。
设置步骤的字体有一些简单的步骤。
1.选择File>New>Folder>Assets Folder
2.点击finish
3.右击assets并创建一个名为fonts
的文件夹4.把你的字体文件放到assets > fonts
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/anyFont.ttf");
//and then use the typeface for changing the font using `textView.setTypeface(tf)`
【讨论】:
要使用外部字体,请先下载 .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" />
【讨论】:
如果您想为您的 android studio ide 使用这些字体,请下载并安装字体并更改 android studio 代码文本字体。
如果您想为创建的 android 应用程序使用字体,则必须将所有 .ttf 文件放在应用程序的资产文件夹中。请谷歌如何创建资产文件夹以及如何在您创建的应用程序中使用自定义字体
【讨论】: