Android 设置字体的三种方法(TypeFace)
2017-04-09 19:02
363 查看
Android系统默认字体支持四种字体,分别为:
noraml (普通字体,系统默认使用的字体)
sans(非衬线字体)
serif (衬线字体)
monospace(等宽字体)
除此之外还可以使用其他字体文件(*.ttf)
1.在xml中修改字体
<!-- 不指明typeface -->
<TextView style="@style/TextStyle"
android:text="Hello , world 中华人民共和国(没有设置属性)"/>
<!-- 使用默认的normal字体-->
<TextView
style="@style/TextStyle"
android:typeface="normal"
android:text="Hello , world 中华人民共和国(xml设置normal)"/>
<!-- 使用默认的sans字体-->
<TextView
style="@style/TextStyle"
android:typeface="sans"
android:text="Hello , world 中华人民共和国(xml设置sans)"/>
<!-- 使用默认的serifs字体-->
<TextView
style="@style/TextStyle"
android:typeface="serif"
android:text="Hello , world 中华人民共和国(xml设置serif)"/>
<!-- 使用默认的monospace字体-->
<TextView
style="@style/TextStyle"
android:typeface="monospace"
android:text="Hello , world 中华人民共和国(xml设置monospace)"/>
2.在Java代码中修改字体
vSansText = (TextView) findViewById(R.id.sans);
vSerifText = (TextView) findViewById(R.id.serif);
vMonospaceText = (TextView) findViewById(R.id.monospace);
//设置字体样式
vSansText.setTypeface(Typeface.SANS_SERIF);
vSerifText.setTypeface(Typeface.SERIF);
vMonospaceText.setTypeface(Typeface.MONOSPACE);
![](https://img-blog.csdn.net/20170409190007836?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGVnZW5kMTIzMDA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
//从asset 读取字体
//得到AssetManager
AssetManager mgr = getAssets();
//根据路径得到Typeface
Typeface tf = Typeface.createFromAsset(mgr, "fonts/HelveticaNeueLTPro-UltLt.otf");
//设置字体
vTTFText.setTypeface(tf);
源码
noraml (普通字体,系统默认使用的字体)
sans(非衬线字体)
serif (衬线字体)
monospace(等宽字体)
除此之外还可以使用其他字体文件(*.ttf)
一、使用系统自带的字体
1.在xml中修改字体<!-- 不指明typeface -->
<TextView style="@style/TextStyle"
android:text="Hello , world 中华人民共和国(没有设置属性)"/>
<!-- 使用默认的normal字体-->
<TextView
style="@style/TextStyle"
android:typeface="normal"
android:text="Hello , world 中华人民共和国(xml设置normal)"/>
<!-- 使用默认的sans字体-->
<TextView
style="@style/TextStyle"
android:typeface="sans"
android:text="Hello , world 中华人民共和国(xml设置sans)"/>
<!-- 使用默认的serifs字体-->
<TextView
style="@style/TextStyle"
android:typeface="serif"
android:text="Hello , world 中华人民共和国(xml设置serif)"/>
<!-- 使用默认的monospace字体-->
<TextView
style="@style/TextStyle"
android:typeface="monospace"
android:text="Hello , world 中华人民共和国(xml设置monospace)"/>
2.在Java代码中修改字体
vSansText = (TextView) findViewById(R.id.sans);
vSerifText = (TextView) findViewById(R.id.serif);
vMonospaceText = (TextView) findViewById(R.id.monospace);
//设置字体样式
vSansText.setTypeface(Typeface.SANS_SERIF);
vSerifText.setTypeface(Typeface.SERIF);
vMonospaceText.setTypeface(Typeface.MONOSPACE);
二、在Android中可以引入其他字体
//从asset 读取字体
//得到AssetManager
AssetManager mgr = getAssets();
//根据路径得到Typeface
Typeface tf = Typeface.createFromAsset(mgr, "fonts/HelveticaNeueLTPro-UltLt.otf");
//设置字体
vTTFText.setTypeface(tf);
源码
相关文章推荐
- Android 设置字体的三种方法(TypeFace)
- android设置字体颜色的三种方法
- Android TextView 设置字体大小的方法
- Android字体Typeface设置
- Android改变字体方法——Typeface
- Android 字体设置 Typeface 设置
- Android 一个TextView中设置文字不同字体大小和颜色的最完整方法
- android中设置TabHost当中的字体的方法
- Android中通过typeface设置字体
- Android中通过typeface设置字体
- Android Studio设置、改变字体和主题的方法
- android改变字体的颜色的三种方法
- Android 控件设置字体【Typeface】
- android中字体设置Typeface的使用
- Android中设置文本颜色的三种方法
- Android TextView字体颜色设置方法小结
- Android中通过typeface设置字体
- android透明度的三种设置方法
- Android 中文字体的设置方法和使用技巧
- Android课程---Android设置透明效果的三种方法(转)