Android 字体库的使用。引入外部字体
2017-09-19 14:24
435 查看
原文地址:http://blog.csdn.net/zuiwuyuan/article/details/49454411
开发Android的人大多都知道,Android里面对字体的支持少得可怜,默认情况下,TextView 的 typeface 属性支持 "Sans","serif","monospace" 这三种字体,如果在没有指定字体的情况下,系统缺省会使用 "Sans" 作为文本显示的字体。但这三种字体只支持英文,也就是说只要你显示的文字是中文,无论你选择这三种字体中的哪一种,显示效果都是一样的。
但这对开发一款精致的APP来说,或许是不够的,毕竟,咱们大家都喜欢用高端大气上档次的字体,吸引用户的眼球。
先给给大家呈现效果图:
是不是字体变化的更上档次了,看下实现吧:
[java] view
plain copy
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView text1;
private TextView text2;
private TextView text3;
private TextView text4;
private TextView text5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1 = (TextView) findViewById(R.id.text1);
text2 = (TextView) findViewById(R.id.text2);
text3 = (TextView) findViewById(R.id.text3);
text4 = (TextView) findViewById(R.id.text4);
text5 = (TextView) findViewById(R.id.text5);
Typeface typeFace1 = Typeface.createFromAsset(getAssets(), "fonts/huaxing.ttf");
Typeface typeFace2 = Typeface.createFromAsset(getAssets(), "fonts/Helvetica.ttf");
Typeface typeFace3 = Typeface.createFromAsset(getAssets(), "fonts/simkai.ttf");
Typeface typeFace4 = Typeface.createFromAsset(getAssets(), "fonts/huacai.TTF");
Typeface typeFace5 = Typeface.createFromAsset(getAssets(), "fonts/fangxiao.TTF");
text1.setTypeface(typeFace1);
text2.setTypeface(typeFace2);
text3.setTypeface(typeFace3);
text4.setTypeface(typeFace4);
text5.setTypeface(typeFace5);
}
}
有人搞不清这些字体库文件放在哪,即"fonts/fangxiao.TTF"在哪,给大家看下目录:
好啦,就是这么easy!
注: 这里的.ttf 大小写一定要区分好。。是什么就怎么引入。字体库虽然很好,但着实不建议使用,why?因为字体库的体积实在庞大,一个简单的库,比如我用了仿宋,APP增加了2.5M;用了宋体,app增加了3M,所以,一般来说不建议使用字体库,除非你的app风格大多要求使用该字体,否则完全没有必要引用字体库,得不偿失!
开发Android的人大多都知道,Android里面对字体的支持少得可怜,默认情况下,TextView 的 typeface 属性支持 "Sans","serif","monospace" 这三种字体,如果在没有指定字体的情况下,系统缺省会使用 "Sans" 作为文本显示的字体。但这三种字体只支持英文,也就是说只要你显示的文字是中文,无论你选择这三种字体中的哪一种,显示效果都是一样的。
但这对开发一款精致的APP来说,或许是不够的,毕竟,咱们大家都喜欢用高端大气上档次的字体,吸引用户的眼球。
先给给大家呈现效果图:
是不是字体变化的更上档次了,看下实现吧:
[java] view
plain copy
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView text1;
private TextView text2;
private TextView text3;
private TextView text4;
private TextView text5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1 = (TextView) findViewById(R.id.text1);
text2 = (TextView) findViewById(R.id.text2);
text3 = (TextView) findViewById(R.id.text3);
text4 = (TextView) findViewById(R.id.text4);
text5 = (TextView) findViewById(R.id.text5);
Typeface typeFace1 = Typeface.createFromAsset(getAssets(), "fonts/huaxing.ttf");
Typeface typeFace2 = Typeface.createFromAsset(getAssets(), "fonts/Helvetica.ttf");
Typeface typeFace3 = Typeface.createFromAsset(getAssets(), "fonts/simkai.ttf");
Typeface typeFace4 = Typeface.createFromAsset(getAssets(), "fonts/huacai.TTF");
Typeface typeFace5 = Typeface.createFromAsset(getAssets(), "fonts/fangxiao.TTF");
text1.setTypeface(typeFace1);
text2.setTypeface(typeFace2);
text3.setTypeface(typeFace3);
text4.setTypeface(typeFace4);
text5.setTypeface(typeFace5);
}
}
有人搞不清这些字体库文件放在哪,即"fonts/fangxiao.TTF"在哪,给大家看下目录:
好啦,就是这么easy!
注: 这里的.ttf 大小写一定要区分好。。是什么就怎么引入。字体库虽然很好,但着实不建议使用,why?因为字体库的体积实在庞大,一个简单的库,比如我用了仿宋,APP增加了2.5M;用了宋体,app增加了3M,所以,一般来说不建议使用字体库,除非你的app风格大多要求使用该字体,否则完全没有必要引用字体库,得不偿失!
相关文章推荐
- android中引入外部字体
- Android 外部字体的引入
- Android引入外部自定义特殊字体的方法
- Android使用外部字体
- android 使用外部字体
- vue 外部字体图标使用,无须绝对路径引入办法
- Android引入外部自定义特殊字体的方法
- Android引入外部字体更改APP字体
- android 使用外部字体
- Android 自定义字体(引入外部字体)
- Android引入外部自定义特殊字体的方法
- Android引入外部自定义特殊字体的方法
- Android 如何在Eclipse 引入外部纯Java项目(不是打成Jar使用)
- Android 引用使用外部字体
- Android引入外部字体更改APP字体
- Android引入外部自定义特殊字体的方法
- Android的TextView/EditText使用CharacterStyle&SpannableString来处理图片显示、字体样式、超链接等
- [Android实例] Android 使用【AIDL】调用外部服务
- 在ANDROID中使用自定义字体
- Android的TextView使用Html来处理图片显示、字体样式、超链接等