Android 中使用自定义ttf字体实现酷炫效果

所谓无图无真相,先看效果图:
Android 中使用自定义ttf字体实现酷炫效果_第1张图片

Java代码如下:

package yc.android.fonts;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;

public class Y_fonts extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                /*
                 * 必须事先在assets底下创建一fonts文件夹 并放入要使用的字体文件(.ttf)
                 * 并提供相对路径给creatFromAsset()来创建Typeface对象
                 */
                Typeface fontFace = Typeface.createFromAsset(getAssets(),
                                "fonts/STXINGKA.TTF");
                // 字体文件必须是true type font的格式(ttf);
                // 当使用外部字体却又发现字体没有变化的时候(以 Droid Sans代替),通常是因为
                // 这个字体android没有支持,而非你的程序发生了错误

                TextView text = (TextView) findViewById(R.id.ttf);
                text.setTypeface(fontFace);
        }
}

PS:
我使用的字体是华文行楷
由于Android系统对字体文件的支持问题,该字体文件2.3.3版本以上有效,2.2版本不支持。

来源:http://www.eoeandroid.com/thread-163834-1-1.html

你可能感兴趣的:(android实用技巧)