收藏 分享(赏)

Android 字体设置.doc

上传人:scg750829 文档编号:12303021 上传时间:2021-12-10 格式:DOC 页数:5 大小:70KB
下载 相关 举报
Android 字体设置.doc_第1页
第1页 / 共5页
Android 字体设置.doc_第2页
第2页 / 共5页
Android 字体设置.doc_第3页
第3页 / 共5页
Android 字体设置.doc_第4页
第4页 / 共5页
Android 字体设置.doc_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

1、深度解析 Android 中字体设置Android 对中文字体支持很不好 需要加入相应的字体库1、在 Android XML 文件中设置字体可以采用 Android:typeface,例如 android:typeface=”monospace”。在这里例子中我们在 Activity 中对 android:text=”Hello, World! 您好”分别进行了四种显示方式,依次为“Sans ”, “serif”, “monospace”和系统缺省方式(经试验缺省采用采用 sans) 。英文字体有差异,貌似中文字体没有差异。XML 文件如下:java 代码:android:textSize=”

2、20sp” / (1 )创建布局 Layout/创建线性布局LinearLayout linearLayout=newLinearLayout(this); /设定线性布局为垂直方向linearLayout.setOrientation(LinearLayout.VERTICAL);/以该线性布局做视图setContentView(linearLayout);(2 )针对正常字体/普通正常字体normal=newTextView(this); /设置字体内容,请注意:目前 Android 主要针对拉丁语系可使用字型设定,中文暂不支持normal.setText(“Normal Font FYI

3、“); /设置字体大小normal.setTextSize(20.0f);/设置字型为默认,正常字体normal.setTypeface(Typeface.DEFAULT,Typeface.NORMAL);/增加该字体并显示到布局 linearLayout 中linearLayout.addView(normal,newLinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);(3 )针对粗体字体 /粗体字体bold=newTextView(this);bold.setText(“Bold Fo

4、nt FYI“);bold.setTextSize(20.0f);/设置字体颜色为蓝色bold.setTextColor(Color.BLUE); /设置字型为默认粗体,粗体字体bold.setTypeface(Typeface.DEFAULT_BOLD, Typeface.BOLD);linearLayout.addView(bold,newLinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);(4 )针对斜体字体/斜体字体italic=newTextView(this);italic.s

5、etTextSize(20f);italic.setText(“Italic Font FYI“); /设置字体颜色为红色italic.setTextColor(Color.RED);/设置字型为等宽字型,斜体字体italic.setTypeface(Typeface.MONOSPACE,Typeface.ITALIC);linearLayout.addView(italic,newLinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);(5 )针对粗斜体字体 /粗斜体字体italic_bol

6、d=newTextView(this);italic_bold.setTextSize(20f);italic_bold.setText(“Italic /设置字体颜色为黄色italic_bold.setTextColor(Color.YELLOW); /设置字型为等宽字型,斜体字体italic_bold.setTypeface(Typeface.MONOSPACE,Typeface.BOLD_ITALIC);linearLayout.addView(italic_bold,newLinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,Layou

7、tParams.WRAP_CONTENT); (6 )针对中文仿“粗体”/针对 Android 字型的中文字体问题chinese=newTextView(this);chinese.setText(“中文粗体显示效果“); /设置字体颜色chinese.setTextColor(Color.MAGENTA);chinese.setTextSize(20.0f);chinese.setTypeface(Typeface.DEFAULT_BOLD, Typeface.BOLD);/使用 TextPaint 的仿“粗体”设置 setFakeBoldText 为 true。目前还无法支持仿“斜体”方法

8、tp=chinese.getPaint();tp.setFakeBoldText(true);linearLayout.addView(chinese,newLinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);2、使用其他字体(自定义创建字型)1)将新字体的 TTF 文件 copy 到 assets/fonts/目录下面,例如我们将 “*.ttf”copy 了过去。2)我们需要将 widget 设置为该字体,比较遗憾的是,不能直接在 XML 文件中进行,需要编写源代码。java 代码:Te

9、xtView tv = (TextView)findViewById(R.id.c12_custom); /从 assert 中获取有资源,获得 app 的 assert,采用 getAserts(),通过给出在 assert/下面的相对路径。在实际使用中,字体库可能存在于 SD 卡上,可以采用 createFromFile()来替代 createFromAsset。 Typeface face = Typeface.createFromAsset (getAssets() , “fonts/timesi.ttf” ); tv.setTypeface (face); 我在模拟器中先后导入华文行

10、楷的字体,大约 4M,但是系统无法识别出该字体,没有显示,然后尝试使用英文字体 timesi.ttf,正常。因此 Android 并非和所有的 TTF 字体都能兼容,尤其在中文特殊字体的支持会存在问题,对于不兼容的字体,Android 不出报错,只是无法正常显示。一般而言我们都会使用系统缺省提供的字体。对于华文行楷字体,我们一开始使用的文件是中文名字,出现报错,后来我们将之改为全小写的英文名称就不会出错,所以在文件命名上需要注意。/自定义字体字型custom=newTextView(this);/字体 MgOpenCosmeticaBold.ttf 放置于 assets/fonts/路径下ty

11、peface=Typeface.createFromAsset(getAssets(),“fonts/MgOpenCosmeticaBold.ttf“);custom.setTypeface(typeface);custom.setText(“Custom Font FYI“);custom.setTextSize(20.0f); /设置字体颜色custom.setTextColor(Color.CYAN);linearLayout.addView(custom,newLinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParam

12、s.WRAP_CONTENT);3、一些注意使用其他字库,都会消耗程序的空间,这是要非常注意的。而且这些字库有时并不能完全提供你所需要的文字。举个例子,省略方式。当文字太多的时候,可以通过省略号省略后面的内容,省略号是使用“”作为一个字体,可通过 android:ellipsize 属性进行设置。如果我们需要使用省略功能,需要确保字体具有省略号。此外,为了保证长度的一直,Android 会进行填充处理,除了将一个字符更换为省略符合外,后面的字符将更换为一个特殊的 Unicode 字符,ZERO WIDTH NO-BREAK SPACE (U+FEFF)。这个字符并占用任何可视的位置,但是保障了 string 具有同样的长度。不是所有的字体都支持这个特殊的字符,可能会引发一些乱码现象。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报