前言:我们做APP的过程中字体有时不能达到我们的要求,那么这时候我们就要进行调整,其实也很简单,我们可以利用别人的ttf文件来获取我们想要的字体。
首先我们想要获取ttf文件:http://pan.baidu.com/s/1c1E7wiO
然后我们可以在APP中使用。
新建assets文件,再在assets下建fonts文件,然后放置资源文件进去,如图:
下面直接可以使用了
String fontsPath = "fonts" + File.separator;
AssetManager assets = getAssets();
typefaces[0] = Typeface.createFromAsset(assets, fontsPath + "one.ttf");
typefaces[1] = Typeface.createFromAsset(assets, fontsPath + "two.ttf");
typefaces[2] = Typeface.createFromAsset(assets, fontsPath + "three.ttf");
typefaces[3] = Typeface.createFromAsset(assets, fontsPath + "four.ttf");
typefaces[4] = Typeface.createFromAsset(assets, fontsPath + "five.ttf");
typefaces[5] = Typeface.createFromAsset(assets, fontsPath + "six.ttf");
typefaces[6] = Typeface.createFromAsset(assets, fontsPath + "seven.ttf");
typefaces[7] = Typeface.createFromAsset(assets, fontsPath + "eight.ttf");
typefaces[8] = Typeface.createFromAsset(assets, fontsPath + "nine.ttf");
typefaces[9] = Typeface.createFromAsset(assets, fontsPath + "ten.ttf");
txt1.setTypeface(typefaces[0]);
txt2.setTypeface(typefaces[1]);
txt3.setTypeface(typefaces[2]);
txt4.setTypeface(typefaces[3]);
txt5.setTypeface(typefaces[4]);
txt6.setTypeface(typefaces[5]);
txt7.setTypeface(typefaces[6]);
txt8.setTypeface(typefaces[7]);
txt9.setTypeface(typefaces[8]);
txt10.setTypeface(typefaces[9]);
大家打开可能有点慢,但是不要紧。
这样就完成了对字体的设置。
除了别人的字体,系统也有三种字体,分别是:
normal bold italic
这几个属性我们一般可以直接在布局文件里面设置,如下:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:gravity="center"
android:textStyle="normal"
android:text="系统normal"/>
但是这样也有点麻烦,如果该页面每个控件都要这么弄不就比较繁琐了么,不要紧,我们还可以在style里面配置,分三种,如下:
<style name="baseTextStyle2">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:fontFamily">sans-serif-light</item>
</style>
<style name="baseTextStyle3">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:fontFamily">sans-serif-condensed</item>
</style>
<style name="baseTextStyle4">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:fontFamily">sans-serif-thin</item>
</style>
这里说一下:
android:fontFamily="sans-serif-light"
android:fontFamily="sans-serif-condensed"condensed
android:fontFamily="sans-serif-thin"(android 4.2)
和
ͬandroid:textStyle="normal|bold|italic"
是相互匹配的。
最后给出源码:http://pan.baidu.com/s/1qYyQdHU