本篇主要讲述的是通过XML文件进行全局字体的修改。文章内容大部分来自Android官方文档。Fonts in XML
Android 8.0(API 26)加入了一个新特性 - Fonts in XML,此特性允许将字体作为资源进行加载,使我们可以更方便的设置全局的字体样式。添加的方式是在 app/res/font 文件夹下添加字体文件(.otf或.ttf文件),然后就像平时使用资源文件的方式使用字体资源。例如,@font/myfont或R.font.myfont。
前提条件
1.这种通过资源文件修改字体的方式只能在运行Android 4.1(API级别16)及更高版本的设备上才能生效。
2.并且你的App需要使用Support Library 26及以上版本。
先给大家看下最终的实际使用与效果
<TextView
style="@style/MaliTextStyle"
android:text="@string/title"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@+id/text"
style="@style/MaliTextStyle"
android:layout_marginTop="20dp"
android:text="@string/text1"
android:textSize="15sp"
android:textStyle="normal" />
<TextView
style="@style/MaliTextStyle"
android:layout_marginTop="20dp"
android:text="@string/text2"
android:textSize="15sp"
android:textStyle="italic" />
运行效果.png
开始敲代码
-
1.右键单击res文件夹,然后转到“ New”>“Android Resource Directory”。
将出现“ New Resource Directory”窗口。 -
2.在“Resource type”列表中,选择“ font”,然后单击“ 确定”。
创建一个新的字体资源目录.png -
3.在字体文件夹中添加字体文件。
创建font文件夹,复制.ttf文件.png
如果你使用的是Android Studio,双击字体文件可以直接预览字体。我这里的字体都是在网上下载的,英文字体文件可以在Google Fonts获取字体文件(需要翻墙)。我选择了其中的两种字体做个示例:Merriweather 与 Mali。
这里需要注意,下载的字体文件都是大写加空格的,需要按照资源文件命名规范进行修改。
- 4.创建字体系列(font-family)
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<font
android:font="@font/merriweather_regular"
android:fontStyle="normal"
android:fontWeight="400"
app:font="@font/merriweather_regular"
app:fontStyle="normal"
app:fontWeight="400"
tools:targetApi="o" />
<font
android:font="@font/merriweather_italic"
android:fontStyle="normal"
android:fontWeight="400"
app:font="@font/merriweather_italic"
app:fontStyle="normal"
app:fontWeight="400"
tools:targetApi="o" />
</font-family>
字体系列是一组字体文件及其样式和权重的详细信息。有时,解压缩字体文件夹,并发现多个版本的字体相同,如斜体版本,或重量和厚度不同的字体。 如果不知道具体的权重可以在Google Fonts的下载页面看到。 字体权重.png
如果是中文字体,权重都设置为100即可。
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<!--这里引用的字体是中文字体-->
<font
android:font="@font/pingfang_regular"
android:fontStyle="normal"
android:fontWeight="100"
app:font="@font/pingfang_regular"
app:fontStyle="normal"
app:fontWeight="100"
tools:targetApi="o"/>
<font
android:font="@font/pingfang_regular"
android:fontStyle="italic"
android:fontWeight="100"
app:font="@font/pingfang_regular"
app:fontStyle="italic"
app:fontWeight="100"
tools:targetApi="o"/>
</font-family>
使用font-family就可以同个一个xml来配置管理定义字体的每个版本,以及相关联的样式和权重属性。
- 5.在布局文件中使用
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/font_merriweather" />
到这里为止,通过XML修改字体已经完成了。
下面讲一讲通过style来更方便的设置字体,也让布局文件更精简。
1.编写width 与 height 的style
2.编写XXXTextStyle,继承Layout_Wrap_Wrap,在Style中还可以设置字间距、行间距,对应属性分别是:letterSpacing、lineSpacingExtra
3.XXXTextStyle还可以用在所有继承自TextView编写的View中,例如:Button、EditText
4.将TextSize 与 TextColor也写在Style中,这样可以规范XML中文本相关的样式编写。
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Layout_Wrap_Wrap">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="Layout_Match_Wrap">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="Layout_Match_Match">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
</style>
<style name="Layout_Wrap_Match">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">match_parent</item>
</style>
<style name="MaliTextStyle" parent="Layout_Wrap_Wrap">
<item name="android:fontFamily">@font/font_mali</item>
<item name="android:letterSpacing" tools:targetApi="lollipop">0.05</item>
</style>
<style name="MerriweatherTextStyle" parent="Layout_Wrap_Wrap">
<item name="android:fontFamily">@font/font_merriweather</item>
<item name="android:letterSpacing" tools:targetApi="lollipop">0.05</item>
</style>
<style name="TextStyle_10_Gray87868B" parent="MaliTextStyle">
<item name="android:textSize">@dimen/sp_10</item>
<item name="android:textColor">@color/app_gray_87868B</item>
</style>
</resources>
<!--布局文件中使用-->
<TextView
style="@style/TextStyle_10_Gray87868B"
android:text="@string/title"/>
这样的Style复用性强,拓展性强,代码阅读性也强
以上的代码已经上传到github:示例代码
网友评论