FontFamily 看这里就够了

article/2025/10/25 7:00:11
  • TextView中字体设置
    • typeface
      • 一、配置设置
      • 代码方式设置
    • 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字体的区别

有两种方式设置字体。

一、配置设置

<TextViewandroid:text="Hello,World"android:textSize="20sp"<!--  使用默认的sans字体-->android:typeface="sans" <!--  使用默认的serifs字体-->   android:typeface="serif"<!--  使用默认的monospace字体-->android:typeface="monospace"/>

代码方式设置

//设置serif字体
textView.setTypeface(Typeface.SERIF);
//设置sans字体
textView.setTypeface(Typeface.SANS_SERIF);
//设置monospace字体
textView.setTypeface(Typeface.MONOSPACE);

fontFamily

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

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

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

    <!-- first font is default --><family name="sans-serif"><font weight="100" style="normal">Roboto-Thin.ttf</font><font weight="100" style="italic">Roboto-ThinItalic.ttf</font><font weight="300" style="normal">Roboto-Light.ttf</font><font weight="300" style="italic">Roboto-LightItalic.ttf</font><font weight="400" style="normal">Roboto-Regular.ttf</font><font weight="400" style="italic">Roboto-Italic.ttf</font><font weight="500" style="normal">Roboto-Medium.ttf</font><font weight="500" style="italic">Roboto-MediumItalic.ttf</font><font weight="900" style="normal">Roboto-Black.ttf</font><font weight="900" style="italic">Roboto-BlackItalic.ttf</font><font weight="700" style="normal">Roboto-Bold.ttf</font><font weight="700" style="italic">Roboto-BoldItalic.ttf</font></family>

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

Font families
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.The complete set of Roboto font variants available is:Regular
Italic
Bold
Bold-italic
Light
Light-italic
Condensed regular
Condensed italic
Condensed bold
Condensed bold-italic
You can apply any one of these with the new fontFamily attribute in combination with the textStyle attribute.Supported values for fontFamily are:"sans-serif" for regular Roboto
"sans-serif-light" for Roboto Light
"sans-serif-condensed" for Roboto Condensed
You can then apply bold and/or italic with textStyle values "bold" and "italic". You can apply both like so: android:textStyle="bold|italic".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" 
  • 方式二:通过代码设置。
TextView.setTypeface(TypeFace typeface)。
public void setTypeface(Typeface tf) {//...其他代码mTextPaint.setTypeface(tf);//...更新界面代码
}

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

TextView.setTypeface(TypeFace typeface, int style)。public void setTypeface(Typeface tf, int style) {if (style > 0) {if (tf == null) {tf = Typeface.defaultFromStyle(style);} else {tf = Typeface.create(tf, style);}setTypeface(tf);// now compute what (if any) algorithmic styling is neededint typefaceStyle = tf != null ? tf.getStyle() : 0;int need = style & ~typefaceStyle;mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0);mTextPaint.setTextSkewX((need & Typeface.ITALIC) != 0 ? -0.25f : 0);} else {mTextPaint.setFakeBoldText(false);mTextPaint.setTextSkewX(0);setTypeface(tf);}
}
  • Paint.setFakeBoldText方法实现字体的加粗
  • Paint.setTextSkewX 实现字体斜体

三者之间的关系

TextView中有个私有方法:setTypefaceFromAttrs

    private void setTypefaceFromAttrs(String familyName, int typefaceIndex, int styleIndex) {Typeface tf = null;if (familyName != null) {tf = Typeface.create(familyName, styleIndex);if (tf != null) {setTypeface(tf);return;}}switch (typefaceIndex) {case SANS:tf = Typeface.SANS_SERIF;break;case SERIF:tf = Typeface.SERIF;break;case MONOSPACE:tf = Typeface.MONOSPACE;break;}setTypeface(tf, styleIndex);}

结论:

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

自定义字体

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

参考:

  • how-to-change-fontfamily-of-textview-in-android
  • Valid values for android:fontFamily and what they map to?
  • 自定义字体

  • https://material.io/guidelines/style/typography.html#typography-line-height

  • https://storage.googleapis.com/material-design/publish/material_v_12/assets/0B0J8hsRkk91LNGdYTEF0VnVPT0k/robotospecimenbooklet.pdf


http://chatgpt.dhexx.cn/article/VN6XQFVe.shtml

相关文章

WPF—WrapPanel布局

WrapPanel布局面板将各个控件从左至右按照行或列的顺序罗列&#xff0c;当长度或高度不够是就会自动调整进行换行&#xff0c;后续排序按照从上至下或从右至左的顺序进行。 Orientation——根据内容自动换行。当 Horizontal选项看上去类似于Windows资源管理器的缩略图视图&…

WrapPanel

WrapPanel将子元素自左向右逐个地排列, 若一个水平行中放不下,就排在下一行。面板的方向可以是水平或垂直的 <Window x:Class"Panel布局.WrapPanelxaml"xmlns"http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x"http://schemas.…

WPF中5种内建面板Canvas、StackPanel、WrapPanel、DockPanel、Grid分析

https://blog.csdn.net/weixin_43437202/article/details/88292620 Canvas、StackPanel、WrapPanel、DockPanel和Grid是WPF中主要的5种内建面板&#xff0c;这些面板类都位于System.Windows.Controls命名空间下。 主要布局特点简述 Grid 按行列排列内容&#xff0c;如果没有特…

WPF 改进 WrapPanel 右侧填充

WPF 改进 WrapPanel 右侧填充 本文经原作者授权以原创方式二次分享&#xff0c;欢迎转载、分享。 WPF 改进 WrapPanel 右边缘填充 作者&#xff1a;陈-林-赵-魏|驚鏵 原文链接[1]&#xff1a;https://www.cnblogs.com/wandia/p/17084881.html 目的&#xff1a;改进下WrapPanel,…

WrapPanel 实现虚拟化

WrapPanel 实现虚拟化 控件名&#xff1a;VirtualizingWrapPanel 作者&#xff1a;WPFDevelopersOrg 原文链接&#xff1a; https://github.com/WPFDevelopersOrg/WPFDevelopers 框架使用大于等于.NET40&#xff1b;Visual Studio 2022;项目使用 MIT 开源许可协议&#xff1…

对wpf 的入门记录总结----面板控件Canvas、WrapPanel、StackPanel、DockPanel

面板是WPF裡其中一個很重要的控件。 面板扮演著裝載其他控件的容器的角色&#xff0c;同時也控制著頁面和視窗的佈局。 由於一個視窗只允許一個子控件&#xff0c;因此面板經常會被使用於分隔空間&#xff0c;這樣每個空間就會有一個控件或者面板。 wpf支持6种面板&#xff1…

listbox 表头自动换行_WPF让ListView或ListBox中的WrapPanel 自动换行

原文:WPF让ListView或ListBox中的WrapPanel 自动换行 在ListView或者ListBox中使用WrapPanel想让ItemTemplate中的内容像下图这样先横向平铺然后再纵向换行&#xff0c;默认设置是不能实现的。 图1. 横向平铺再纵向换行的效果示例 请注意最关键的在上面代码中第一行的“ScrollV…

WPF教程三:布局之WrapPanel面板(转 )

WPF教程三&#xff1a;布局之WrapPanel面板 WrapPanel&#xff1a;环绕面板 WrapPanel布局面板将各个控件从左至右按照行或列的顺序罗列&#xff0c;当长度或高度不够时就会自动调整进行换行&#xff0c;后续排序按照从上至下或从右至左的顺序进行。 Orientation——根据内容自…

WPF面板布局介绍Grid、StackPanel、DockPanel、WrapPanel

WPF面板布局介绍Grid、StackPanel、DockPanel、WrapPanel 回顾 上一篇&#xff0c;我们介绍了基本控件及控件的重要属性和用法&#xff0c;我们本篇详细介绍WPF中的几种布局容器及每种布局容器的使用场景&#xff0c;当 然这些都是本人在实际项目中的使用经验&#xff0c;可能…

2021-08-16 WPF控件专题 WrapPanel 控件详解

1.WrapPanel 控件介绍 流面板 子元素按顺序排列&#xff0c;如果按水平方向:从左到右&#xff0c;超出部分&#xff0c;自动换行到下一行 垂直 从上到下&#xff0c; 下一列 排列方向&#xff1a;Orientation ItemWidth ItemHeight 调整面板的尺寸时&#xff0c;内部子元素的…

.NET WPF(3)布局之WrapPanel面板

一.WPF&#xff08;3&#xff09;布局之WrapPane面板 1. WrapPanel&#xff1a;环绕面板 WrapPanel布局面板将各个控件从左至右按照行或列的顺序罗列&#xff0c;当长度或高度不够时就会自动调整进行换行&#xff0c;后续排序按照从上至下或从右至左的顺序进行。 Orie…

WPF编程基础入门 ——— 第三章 布局(五)布局面板WrapPanel

WPF布局——布局面板WrapPanel WPF——WrapPanel布局控件WrapPanel实例——十个按钮 WPF——WrapPanel布局控件 WrapPanel(自动折行面板)&#xff0c;允许任意多的子元素按照声明的先后顺序&#xff0c;从左往右摆放&#xff0c;摆满一行后&#xff0c;自动折行。折行面板的经…

WPF布局控件之WrapPanel

WrapPanel WrapPanel&#xff0c;英文意思是折叠容器&#xff0c;那到底是怎么个折叠法呢&#xff1f;如下&#xff1a; <Window x:Class"LearnLayout.WrapPanelWin"xmlns"http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x"ht…

.NET WPF教程(7)——布局介绍WrapPanel与StackPanel(②)

三. WrapPanel WrapPanel布局面板将各个控件从左至右按照行或列的顺序罗列&#xff0c;当长度或高度不够是就会自动调整进行换行&#xff0c;后续排序按照从上至下或从右至左的顺序进行。 Orientation——根据内容自动换行。当 Horizontal选项看上去类似于Windows资源管理器…

WPF 用代码实现WrapPanel右侧自动对齐(解决多余空白问题)

未处理前效果&#xff1a; 处理后效果&#xff1a; <Border Background"{StaticResource BorderBg}" BorderThickness"2" BorderBrush"{StaticResource BorderBrush}" CornerRadius"5" Padding"5" x:Name"SvK…

C# Grid StackPanel DockPanel WrapPanel

WPF面板布局介绍Grid、StackPanel、DockPanel、WrapPanel 回顾 上一篇&#xff0c;我们介绍了基本控件及控件的重要属性和用法&#xff0c;我们本篇详细介绍WPF中的几种布局容器及每种布局容器的使用场景&#xff0c;当 然这些都是本人在实际项目中的使用经验&#xff0c;可能…

WPF基础五:UI①布局元素WrapPanel

目录 WrapPanel WrapPanel类 XAML范例&#xff1a; C#代码 WrapPanel 按从左到右的顺序位置定位子元素&#xff0c;在包含框的边缘处将内容切换到下一行。 后续排序按照从上至下或从右至左的顺序进行&#xff0c;具体取决于 Orientation 属性的值。 WrapPanel包含UIElemen…

一个优化奇怪的 WrapPanel

一个优化奇怪的 WrapPanel 本文经原作者授权以原创方式二次分享&#xff0c;欢迎转载、分享。 一个优化奇怪的 WrapPanel 作者&#xff1a;陈-林-赵-魏 原文链接[1]&#xff1a;https://www.cnblogs.com/wandia/p/17092221.html FixToRB 附加属性&#xff0c;固定到【右边(水平…

WPF布局之WrapPanel与StackPanel

转载&#xff1a;https://www.cnblogs.com/Im-Victor/p/10565030.html 三. WrapPanel WrapPanel布局面板将各个控件从左至右按照行或列的顺序罗列&#xff0c;当长度或高度不够是就会自动调整进行换行&#xff0c;后续排序按照从上至下或从右至左的顺序进行。 Orientation——…

WrapPanel在不同页面渲染使用

WrapPanel渲染的使用 WrapPanel在UserControl用户控件的使用 页面控件的是渲染出来&#xff0c;在UserControl用户控件的使用无法直接使用&#xff0c;使用会出现空&#xff0c;没法渲染获取到WrapPanel控件。 没法渲染获取效果图&#xff1a; 想成功获取渲染获取到WrapPane…