Android 4.1带来了几种变体和Roboto风格等10种变体字体,并且它们都能被应用程序所使用。你的应用程序现在可以访问全系列的细体和长体字体。
所有可用的的Roboto变体字有:
- 常规<Regular>
- 斜体<Italic>
- 粗体<Bold>
- 粗斜体<Bold-italic>
- 细体<Light>
- 细斜体<Light-italic>
- 常规长体<Condensed regular>
- 长斜体<Condensed italic>
- 长粗体<Condensed bold>
- 长粗斜体<Condensed bold-italic>
4.1新增加的字体:
Roboto-LightItalic.ttf
Roboto-Light.ttf
这两个字库是比较细,颜色比较轻一点 拉丁字库。
AndroidEmoji.ttf
DroidNaskh-Regular-SystemUI.ttf
DroidSansArabic.ttf
DroidSansDevanagari-Regular.ttf
DroidSansTamil-Regular.ttf
DroidSansTamil-Bold.ttf
同事还多了一个XML文件:fallback_fonts-ja.xml
你可以通过新的fontFamily属性与textStyle属性结合,应用上面的任何一种字体fontFamily所支持的值有:
- "scans-serif"为常规的Roboto字体
- "scans-serif-light"为细体的Roboto字体
"scans-serif-condensed"为长体的Roboto字体
然后可以通过textStyle应用粗体和斜体,其值分别为"bold"和"italic"。你可以通过以下方法同时应用两种字体:android:textStyle="bold|italic"。
你也可以使用Typeface.create()方法。例如,Typeface.create("sans-serif-light", Typeface.NORMAL)
下面将列举几个简单的编程例子:
(1)针对粗斜体字体
//粗斜体字体
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));
(2)针对中文仿“粗体”
//针对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));
(3)自定义创建字型
在assets文件目录下创建font目录,再将要使用的字库添加到font目录下,接着在原代码中:
Typeface enFont1 = Typeface.createFromAsset(getAssets(), "font/DroidSansFallback.ttf");
Typeface enFont2 = Typeface.createFromAsset(getAssets(), "font/HelveticaNeueOTS.ttf");
Typeface enFont3 = Typeface.createFromAsset(getAssets(), "font/MYingHeiBold18030C.ttf");
mTextView_EN1.setTypeface(enFont1);
mTextView_EN2.setTypeface(enFont2);
mTextView_EN3.setTypeface(enFont3);