Android 字体库的使用
2015-10-27 22:04
597 查看
开发Android的人大多都知道,Android里面对字体的支持少得可怜,默认情况下,TextView 的 typeface 属性支持 "Sans","serif","monospace" 这三种字体,如果在没有指定字体的情况下,系统缺省会使用 "Sans" 作为文本显示的字体。但这三种字体只支持英文,也就是说只要你显示的文字是中文,无论你选择这三种字体中的哪一种,显示效果都是一样的。
但这对开发一款精致的APP来说,或许是不够的,毕竟,咱们大家都喜欢用高端大气上档次的字体,吸引用户的眼球。
先给给大家呈现效果图:
![](https://img-blog.csdn.net/20151027215837361)
是不是字体变化的更上档次了,看下实现吧:
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"在哪,给大家看下目录:
![](https://img-blog.csdn.net/20151027220138040)
好啦,就是这么easy!
注: 字体库虽然很好,但着实不建议使用,why?因为字体库的体积实在庞大,一个简单的库,比如楷体,都要4M,所以,一般来说不建议使用字体库,除非你的app风格大多要求使用该字体,否则完全没有必要引用字体库,得不偿失!
但这对开发一款精致的APP来说,或许是不够的,毕竟,咱们大家都喜欢用高端大气上档次的字体,吸引用户的眼球。
先给给大家呈现效果图:
是不是字体变化的更上档次了,看下实现吧:
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!
注: 字体库虽然很好,但着实不建议使用,why?因为字体库的体积实在庞大,一个简单的库,比如楷体,都要4M,所以,一般来说不建议使用字体库,除非你的app风格大多要求使用该字体,否则完全没有必要引用字体库,得不偿失!
相关文章推荐
- Android 字体库的使用
- Android 无线调试
- Android启动模式之lunchMode
- android studio设置debug.keystore
- ViewDragHelper详解
- android实现自定义进度条
- android 61 logcat
- Android 中的 onInterceptTouchEvent() 和 onTouchEvent()
- android标题栏上面弹出提示框(二) PopupWindow实现,带动画效果
- android标题栏上面弹出提示框(二) PopupWindow实现,带动画效果
- android 60 发送短信
- Android之3.5版的百度地图的定位、覆盖物实现以及覆盖物的点击事件
- Android中shape的使用
- Android —— 下拉刷新和上拉加载功能实现
- GalleryActivity读取本地相册及照片 能够按相册区分 并选取照片
- Material Design Support Library中NavigationView添加下划线
- android.view.InflateException: Binary XML file line #247: Error inflating class <unknown>
- 【Android学习笔记】属性动画基础学习笔记
- 不可不知的android开发冷知识1
- [Android Pro] Android 4.1 使用 Accessibility实现免Root自动批量安装功能