Android 应用中加载自定义字体可以提升用户体验和品牌形象。Android 提供了多种方式来加载和使用自定义字体,包括使用资源文件、XML 字体声明以及代码动态加载。以下是详细的步骤和示例代码,帮助你在 Android 项目中加载和使用自定义字体。
方法一:使用资源文件
1. 添加字体文件
首先,将你的自定义字体文件(例如 MyCustomFont.ttf 或 MyCustomFont.otf)添加到项目的 res/font 目录中。如果没有 font 目录,请手动创建它。
app/
res/
font/
MyCustomFont.ttf
2. 在 XML 中声明字体
在 res/values/styles.xml 或 res/values/themes.xml 文件中声明一个样式,并指定自定义字体。
示例 styles.xml
示例 themes.xml
3. 在布局文件中使用字体
在布局文件中使用自定义字体样式。
示例 activity_main.xml
android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, World!" android:textSize="24sp" android:textStyle="bold" android:textColor="#000000" style="@style/TextAppearance.CustomFont" />
方法二:直接在 XML 中引用字体
你也可以直接在布局文件中引用自定义字体,而不需要在 styles.xml 中声明样式。
示例 activity_main.xml
android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, World!" android:textSize="24sp" android:textStyle="bold" android:textColor="#000000" android:fontFamily="@font/my_custom_font" />
方法三:使用 Font Resource File
你可以创建一个单独的字体资源文件来管理多个字体变体(如粗体、斜体等)。
1. 创建字体资源文件
在 res/font 目录中创建一个 XML 文件来定义字体资源。
my_custom_font.xml
android:fontStyle="normal" android:fontWeight="400" android:font="@font/my_custom_font_regular" />
android:fontStyle="italic" android:fontWeight="400" android:font="@font/my_custom_font_italic" />
android:fontStyle="normal" android:fontWeight="700" android:font="@font/my_custom_font_bold" /> 2. 在布局文件中使用字体资源 在布局文件中使用定义的字体资源。 activity_main.xml android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, World!" android:textSize="24sp" android:textStyle="bold" android:textColor="#000000" android:fontFamily="@font/my_custom_font" /> android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Italic Text" android:textSize="24sp" android:textStyle="italic" android:textColor="#000000" android:fontFamily="@font/my_custom_font" /> android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bold Text" android:textSize="24sp" android:textStyle="bold" android:textColor="#000000" android:fontFamily="@font/my_custom_font" /> 方法四:使用代码动态加载字体 如果你需要在运行时动态加载字体,可以使用 Typeface 类。 1. 加载字体并在 TextView 中使用 在 Activity 或 Fragment 中加载自定义字体并应用到 TextView。 import android.graphics.Typeface; import android.os.Bundle; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Load the custom font Typeface customFont = Typeface.createFromAsset(getAssets(), "fonts/MyCustomFont.ttf"); // Find the TextView and apply the custom font TextView textView1 = findViewById(R.id.textView1); textView1.setTypeface(customFont); } } 进入代码分屏 2. 确保字体文件路径正确 确保字体文件放在 assets/fonts 目录中。 app/ src/ main/ assets/ fonts/ MyCustomFont.ttf 总结 通过上述方法,你可以在 Android 应用中轻松加载和使用自定义字体。以下是每种方法的简要总结: 使用资源文件: 将字体文件放入 res/font 目录。在 styles.xml 或 themes.xml 中声明样式。在布局文件中使用样式或直接引用字体。 直接在 XML 中引用字体: 将字体文件放入 res/font 目录。在布局文件中直接使用 android:fontFamily 属性。 使用 Font Resource File: 创建一个字体资源文件来管理多个字体变体。在布局文件中使用定义的字体资源。 使用代码动态加载字体: 将字体文件放入 assets/fonts 目录。使用 Typeface.createFromAsset 动态加载字体。应用到相应的 UI 组件。