FontFamily 看这里就够了

TextView中字体设置

textView中有三个属性可以设置字体

  • textStyle:设置样式
  • fontFamily:设置使用的字体
  • typeface:设置使用的字体文件。

textStyle 如下样式

  • NORMAL:默认字体;
  • BOLD:粗题;
  • ITALIC:斜体;
  • BOLD_ITALIC:粗斜体;

typeface

java系统默认支持如下字体:

  • noraml (普通字体,系统默认使用的字体)
  • sans(非衬线字体)
  • serif (衬线字体)
  • monospace(等宽字体)

简单介绍三种字体的区别:在西方国家罗马字母阵营中,字体分为两大种类:Sans Serif和Serif,打字机体虽然也属于Sans Serif,但由于是等宽字体,所以另外独立出Monospace这一种类。

Serif的意思是,在字的笔划开始及结束的地方有额外的装饰,而且笔划的粗细会因直横的不同而有不同。相反的,Sans Serif则没有这些额外的装饰,笔划粗细大致差不多。如下图:

Serif和Sans-serif字体的区别

有两种方式设置字体。

一、配置设置
  1. <TextView
  2. android:text= "Hello,World"
  3. android:textSize= "20sp"
  4. <! -- 使用默认的 sans字体 -->
  5. android:typeface="sans"
  6. <!-- 使用默认的serifs字体-->
  7. android:typeface="serif"
  8. <!-- 使用默认的monospace字体-->
  9. android:typeface="monospace"
  10. />
代码方式设置
  1. //设置serif字体
  2. textView.setTypeface(Typeface.SERIF);
  3. //设置sans字体
  4. textView.setTypeface(Typeface.SANS_SERIF);
  5. //设置monospace字体
  6. textView.setTypeface(Typeface.MONOSPACE);

fontFamily

fontFamily 值为系统支持的字体名称。
根据MATERIAL DESIGN 文档中的排版一章描述,anroid可支持下图的字体样式:

新版本支持新的字体,详细见下表。

这些字体文件定义在系统文件的data/fonts/fonts.xml文件夹下android 5.0 +以上。详细信息可连接的文件。

  1. <!-- first font is default -->
  2. <family name="sans-serif">
  3. <font weight="100" style="normal">Roboto-Thin.ttf </font>
  4. <font weight="100" style="italic">Roboto-ThinItalic.ttf </font>
  5. <font weight="300" style="normal">Roboto-Light.ttf </font>
  6. <font weight="300" style="italic">Roboto-LightItalic.ttf </font>
  7. <font weight="400" style="normal">Roboto-Regular.ttf </font>
  8. <font weight="400" style="italic">Roboto-Italic.ttf </font>
  9. <font weight="500" style="normal">Roboto-Medium.ttf </font>
  10. <font weight="500" style="italic">Roboto-MediumItalic.ttf </font>
  11. <font weight="900" style="normal">Roboto-Black.ttf </font>
  12. <font weight="900" style="italic">Roboto-BlackItalic.ttf </font>
  13. <font weight="700" style="normal">Roboto-Bold.ttf </font>
  14. <font weight="700" style="italic">Roboto-BoldItalic.ttf </font>
  15. </family>

android 4.4- 文件是不同的,感觉更易于理解。android 4.1 介绍中的Font families章节有较为详细的介绍

  1. Font families
  2. Android 4.1 adds several more variants of the Roboto font style for a total of 10 variants, and they're all usable by apps. Your apps now have access to the full set of both light and condensed variants.
  3. The complete set of Roboto font variants available is:
  4. Regular
  5. Italic
  6. Bold
  7. Bold-italic
  8. Light
  9. Light-italic
  10. Condensed regular
  11. Condensed italic
  12. Condensed bold
  13. Condensed bold-italic
  14. You can apply any one of these with the new fontFamily attribute in combination with the textStyle attribute.
  15. Supported values for fontFamily are:
  16. "sans-serif" for regular Roboto
  17. "sans-serif-light" for Roboto Light
  18. "sans-serif-condensed" for Roboto Condensed
  19. You can then apply bold and/ or italic with textStyle values "bold" and "italic". You can apply both like so: android:textStyle= "bold|italic".
  20. You can also use Typeface.create(). For example, Typeface.create( "sans-serif-light", Typeface.NORMAL).

font.xml文件定义了很多字体,若字体不支持怎么办?android 5.0版本的font.xml注释中有如下一段话可以很好的说明系统的处理流程:

The first family is also the default font, which handles font request that have not specified
specific font names.

font.xml文件及其变更记录有较为完备的更新记录。

这里有一张表很好的说明了问题(来源):

Fontandroid:fontFamilyandroid:textStyle
Roboto Thinsans-serif-thin 
Roboto Lightsans-serif-light 
Roboto Regularsans-serif 
Roboto Boldsans-serifbold
Roboto Mediumsans-serif-medium 
Roboto Blacksans-serif-black 
Roboto Condensed Lightsans-serif-condensed-light 
Roboto Condensed Regularsans-serif-condensed 
Roboto Condensed Mediumsans-serif-condensed-medium 
Roboto Condensed Boldsans-serif-condensedbold
Noto Serifserif 
Noto Serif Boldserifbold
Droid Sans Monomonospace 
Cutive Monoserif-monospace 
Coming Sooncasual 
Dancing Scriptcursive 
Dancing Script Boldcursivebold
Carrois Gothic SCsans-serif-smallcaps 

总结: fontFamliy 表示android系统支持的一些列字体。每个字体都一个别名,以唯一标识这个字体。由于android各个版本的不同,会导致高版本支持的某些字体设置在低版本不生效,则使用低版本的默认字体。

文字如何实现加粗、斜体?

我们知道TextView画文字,最终都是通过Paint相关设置来完成的。

android中常用的需求是设置TextView的FontFamily属性。有两种方式设置:

  • 方式一 通过配置文件完成
 android:fontFamily="serif" 
  • 方式二:通过代码设置。
  1. TextView.setTypeface(TypeFace typeface)。
  2. public void setTypeface(Typeface tf) {
  3. //...其他代码
  4. mTextPaint.setTypeface(tf);
  5. //...更新界面代码
  6. }

可以看下TexTView的setTypeface方法具体实现。

  1. TextView.setTypeface(TypeFace typeface, int style)。
  2. public void setTypeface(Typeface tf, int style) {
  3. if (style > 0) {
  4. if (tf == null) {
  5. tf = Typeface.defaultFromStyle(style);
  6. } else {
  7. tf = Typeface.create(tf, style);
  8. }
  9. setTypeface(tf);
  10. // now compute what (if any) algorithmic styling is needed
  11. int typefaceStyle = tf != null ? tf.getStyle() : 0;
  12. int need = style & ~typefaceStyle;
  13. mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0);
  14. mTextPaint.setTextSkewX((need & Typeface.ITALIC) != 0 ? - 0.25f : 0);
  15. } else {
  16. mTextPaint.setFakeBoldText( false);
  17. mTextPaint.setTextSkewX( 0);
  18. setTypeface(tf);
  19. }
  20. }
  • Paint.setFakeBoldText方法实现字体的加粗
  • Paint.setTextSkewX 实现字体斜体

三者之间的关系

TextView中有个私有方法:setTypefaceFromAttrs

  1. private void setTypefaceFromAttrs(String familyName, int typefaceIndex, int styleIndex) {
  2. Typeface tf = null;
  3. if (familyName != null) {
  4. tf = Typeface.create(familyName, styleIndex);
  5. if (tf != null) {
  6. setTypeface(tf);
  7. return;
  8. }
  9. }
  10. switch (typefaceIndex) {
  11. case SANS:
  12. tf = Typeface.SANS_SERIF;
  13. break;
  14. case SERIF:
  15. tf = Typeface.SERIF;
  16. break;
  17. case MONOSPACE:
  18. tf = Typeface.MONOSPACE;
  19. break;
  20. }
  21. setTypeface(tf, styleIndex);
  22. }

结论:

  • fontFamily、typeface 都用于设置字体,d都设置是优先使用fontFamily。
  • textStyle 用于改变字体的样式

自定义字体

android O 开始支持自定义字体
https://developer.android.com/guide/topics/resources/font-resource.html

参考:

原文链接:https://blog.csdn.net/yuanxw44/article/details/80019501
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值