android字体样式
Android 的默认字体
Android 系统默认使用的是一款叫做 Robote 的字体。Robote 本身就是 Google 自己的字体格式,Android 和 Chrome 操作系统上,默认都会使用 Robote 字体,并且也是 Google 视觉语言推荐的字体。
想要了解更多关于 Robote 的内容,可以去 Google 的网站上查看。
正常来说,Robote 就已经提供了多种的选择,例如:粗细、斜体 等等。但是通常它并不能满足我们设计师的需要。
需要注意的是,内置的一些字体只对英文有效。
哪些属性可以影响字体
Android 本身已经提供了一些修改字体样式的属性和方法。
当你想要修改字体的时候,你将面对三个属性,它们都有对应的 Java 方法。
- android:textStyle
- android:typeface
- android:fontFamily
android:textStyle
textStyle
主要用于设定一些字体的样式,它是对所有的字体都生效的。也就是说哪怕你替换了字体,依然可以使用 textStyle
来修饰它的样式。textStyle 本身支持的可选项有 normal|bold|italic
,它们也非常的好理解,就是普通|粗体|斜体
。
android:typeface
typeface
可以用于设置一些默认的字体,它可选的属性有 normal|sans|serif|monospace
等。normal 和 sans 的字体其实是一样的,serif 是一个带衬线的字体,而 nonospace 是等宽字体。
serif 在默认的字体上,增加了衬线。而 nonospace 限制了每个字符的宽度,让它们达到一个等宽的效果。
android:fontFamily
fontFamily
看着像是对 typeface
的一次加强,从它的可选项就能看出来,它更细致的区分了字体的样式。fontFamily
的可选项还是很多的,这里就不一一列举了。不过需要注意,有一些字体的设置是有版本限制的。
例如:sans-serif-medium
是需要 Android 5.0 的版本才支持的。
需要注意的是,如果同时配置了 typeface
和 fontFamily
,将使用 fontFamily
配置的字体。
利用主题修改全局字体
如果你能说服你的设计师,接受系统字体的话,你可以在 Theme 中,去配置你需要的默认字体。
在 application 中,通过 android:theme
来配置一个 App 的主题。一般新创建的项目,都是 @style/AppTheme
。在其中追加关于字体的属性 android:fontFamily
,它就可以完成对全局设置一个系统字体。当然你可以对一个单独的 TextView 配置一个特别的字体,都是可以接受的。
使用自定义字体
字体文件,通常都是设计师提供给我们的,一般都是 .ttf(TrueType)
或者 .otf(OpenType)
这两种格式的,比较常用的是 .ttf
格式的。
通常我们会把字体文件放再 assets 目录下,想要加载字体文件,需要使用到 Typeface 这个类,它其中提供了一些 Api ,用于帮助我们来加载一个我们自定义的字体文件。
例如下面的例子,是一个通用的做法。
Andorid 8.0+ 在资源中定义使用字体
- Right-click the res folder and go to New > Android resource directory.The New Resource Directory window appears.
- In the Resource type list, select font, and then click OK.
- Add your font files in the font folder.
- Double-click a font file to preview the file's fonts in the editor.
Creating a font family
-
Right-click the
font
folder and go to New > Font resource file. The New Resource File window appears. -
Enter the file name, and then click OK. The new font resource XML opens in the editor.
-
Enclose each font file, style, and weight attribute in the
<font>
element. The following XML illustrates adding font-related attributes in the font resource XML:<?xml version="1.0" encoding="utf-8"?> <font-family xmlns:android="http://schemas.android.com/apk/res/android"> <font android:fontStyle="normal" android:fontWeight="400" android:font="@font/lobster_regular" /> <font android:fontStyle="italic" android:fontWeight="400" android:font="@font/lobster_italic" /> </font-family>
Adding fonts to a TextView
In the layout XML file, set the fontFamily
attribute to the font file you want to access.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lobster"/>
Using the support library
The Support Library 26.0 provides support to the Fonts in XML feature on devices running Android 4.1 (API level 16) and higher.
Note: When you declare font families in XML layout through the support library, use the app namespace to ensure your fonts load.
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font app:fontStyle="normal" app:fontWeight="400" app:font="@font/myfont-Regular"/>
<font app:fontStyle="italic" app:fontWeight="400" app:font="@font/myfont-Italic" />
</font-family>
To retrieve fonts programmatically, call the ResourceCompat.getFont(Context, int)
method and provide an instance of Context and the resource identifier.
Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);
思源黑体字体高度异常问题
(Android思源字体高度问题研究)[https://zhuanlan.zhihu.com/p/28012968]
//解决字体高度异常问题 android 5.0+ textView.setElegantTextHeight(true);
(Android TextView高度和字体高度不一致)[https://blog.csdn.net/pxcz110112/article/details/82691001]
android:includeFontPadding="false"