android中要设定用户自己的字体很简单。
1.把自己的字体文件放入assets目录下
2.加载字体
3.给View设置字体
至于字体文件从哪里来就不多说了,可以自己制作也可以从论坛上下载。
第一步也很简单过。
直接上第二部加载字体,这里看一下android的Typeface(字体)类,下面的几个就是加载字体的静态方法。
1 public static Typeface create(String familyName, int style) {
2 return new Typeface(nativeCreate(familyName, style));
3 }
4
5 public static Typeface create(Typeface family, int style) {
6 int ni = 0;
7 if (family != null) {
8 ni = family.native_instance;
9 }
10 return new Typeface(nativeCreateFromTypeface(ni, style));
11 }
12
13
14 public static Typeface createFromAsset(AssetManager mgr, String path) {
15 return new Typeface(nativeCreateFromAsset(mgr, path));
16 }
17
18 public static Typeface createFromFile(File path) {
19 return new Typeface(nativeCreateFromFile(path.getAbsolutePath()));
20 }
21
22 public static Typeface createFromFile(String path) {
23 return new Typeface(nativeCreateFromFile(path));
24 }
2 return new Typeface(nativeCreate(familyName, style));
3 }
4
5 public static Typeface create(Typeface family, int style) {
6 int ni = 0;
7 if (family != null) {
8 ni = family.native_instance;
9 }
10 return new Typeface(nativeCreateFromTypeface(ni, style));
11 }
12
13
14 public static Typeface createFromAsset(AssetManager mgr, String path) {
15 return new Typeface(nativeCreateFromAsset(mgr, path));
16 }
17
18 public static Typeface createFromFile(File path) {
19 return new Typeface(nativeCreateFromFile(path.getAbsolutePath()));
20 }
21
22 public static Typeface createFromFile(String path) {
23 return new Typeface(nativeCreateFromFile(path));
24 }
我们这里使用方法如下:
1 Typeface typeface = Typeface.createFromAsset(getAssets(), "font/Flubber.ttf");
2
3 TextView view = (TextView) findViewById(R.id.font_test);
4 view.setTypeface(typeface);
2
3 TextView view = (TextView) findViewById(R.id.font_test);
4 view.setTypeface(typeface);
效果图片如下
字体文件代码在附件中