Android 字体设置

上传人:s9****2 文档编号:431983900 上传时间:2022-08-06 格式:DOC 页数:5 大小:70KB
返回 下载 相关 举报
Android 字体设置_第1页
第1页 / 共5页
Android 字体设置_第2页
第2页 / 共5页
Android 字体设置_第3页
第3页 / 共5页
Android 字体设置_第4页
第4页 / 共5页
Android 字体设置_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《Android 字体设置》由会员分享,可在线阅读,更多相关《Android 字体设置(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=”20sp” / (1)创建布局

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

3、mal.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 Font FYI); bo

4、ld.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.setTex

5、tSize(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_bold

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

7、ms.WRAP_CONTENT,LayoutParams.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为t

8、rue。目前还无法支持仿“斜体”方法 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文件中进行,需要编写源代码

9、。java代码:TextView 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/路径下 typeface=

11、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,LayoutParams.WRAP_CONTENT);3、一些注意使用其他字库,都会消耗程序的空间,这是要非常注意的。而且这些字库有时并不能完全提供你所需要的文字。举个例子,省略方式。当文字太多的时候,可以通过省略号省略后面的内容,省略号是使用“”作为一个字体,可通过android:ellipsize属性进行设置。如果我们需要使用省略功能,需

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号