Android API之Typeface代码演示
2015-04-28 08:59
204 查看
之前北京一位Android开发者发起翻译Android API的倡议,集中国Android开发者每一个人之力来不断壮大,为一些英语水平一般无法全面理解原文Android API的开发者提供一个桥梁。毕竟,语言不应当是开发者的一个阻碍。
类Typeface详细规定了字体的字型和固有特性。当绘制(和量测)时,使用画笔尤其是在任意使用像textSize,
textSkewX, textScaleX用来指定文字显示,这是很有帮助的。
演示范例(为个人原创,当然遇见问题时参考了一些国内/国外的一些范例):
(1)创建布局Layout
//创建线性布局
LinearLayout linearLayout=newLinearLayout(this);
//设定线性布局为垂直方向
linearLayout.setOrientation(LinearLayout.VERTICAL);
//以该线性布局做视图
setContentView(linearLayout);
(2)针对正常字体
//普通正常字体
normal=newTextView(this);
//设置字体内容,请注意:目前Android主要针对拉丁语系可使用字型设定,中文暂不支持
normal.setText("Normal Font FYI");
//设置字体大小
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 Font 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.setTextSize(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=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(LayoutParams.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为true。目前还无法支持仿“斜体”方法
tp=chinese.getPaint();
tp.setFakeBoldText(true);
linearLayout.addView(chinese,newLinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
(7)自定义创建字型
//自定义字体字型
custom=newTextView(this);
//字体MgOpenCosmeticaBold.ttf放置于assets/font/路径下
typeface=Typeface.createFromAsset(getAssets(),"font/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));
类Typeface详细规定了字体的字型和固有特性。当绘制(和量测)时,使用画笔尤其是在任意使用像textSize,
textSkewX, textScaleX用来指定文字显示,这是很有帮助的。
演示范例(为个人原创,当然遇见问题时参考了一些国内/国外的一些范例):
(1)创建布局Layout
//创建线性布局
LinearLayout linearLayout=newLinearLayout(this);
//设定线性布局为垂直方向
linearLayout.setOrientation(LinearLayout.VERTICAL);
//以该线性布局做视图
setContentView(linearLayout);
(2)针对正常字体
//普通正常字体
normal=newTextView(this);
//设置字体内容,请注意:目前Android主要针对拉丁语系可使用字型设定,中文暂不支持
normal.setText("Normal Font FYI");
//设置字体大小
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 Font 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.setTextSize(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=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(LayoutParams.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为true。目前还无法支持仿“斜体”方法
tp=chinese.getPaint();
tp.setFakeBoldText(true);
linearLayout.addView(chinese,newLinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
(7)自定义创建字型
//自定义字体字型
custom=newTextView(this);
//字体MgOpenCosmeticaBold.ttf放置于assets/font/路径下
typeface=Typeface.createFromAsset(getAssets(),"font/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));
相关文章推荐
- Android API之Typeface代码演示(转)
- Android API之Typeface代码演示
- Android API之ImageView.ScaleType代码演示
- 最简单的换肤(模板替换)应用技术为js+css(附加演示代码)
- RabbitMQ系列—RabbitMQ 代码演示
- ip查询演示代码
- Matlab遗传算法优化问题求解的演示样例代码
- 我自己写的一个分页控件(源码和演示代码)PostBack分页版 for vs2003、SQL Server
- 设计模式:装饰模式的原理及代码演示
- 回调演示的伪代码
- 判断Set里的元素是否重复、==、equals、hashCode方法研究-代码演示
- Hive的各种排序详解与代码演示
- PayPal站点的远程代码执行漏洞演示【附视频】
- TipsWindown弹出层代码演示地址
- Java三大器之监听器(Listener)的工作原理和代码演示
- 10分钟理解Android数据库的创建与使用(附具体解释和演示样例代码)
- ip查询演示代码
- 数据库操作:java连接ORACLE实例代码演示
- IO流 文件读取, 写入,和复制txt文件代码演示
- 从头说12种排序算法:原理、图解、动画视频演示、代码以及笔试面试题目中的应用