Android自定义TTF字体

前言:

  在Android Design中一个设计手册。在设计手册中有常用的UI图标,图标大小规范等。

其中,有一个TTF字体,以前感觉没什么用。但是我在学习时,常看到有许多开发者使用Google

提供的TTF字体。我数了一下有19个字体可供选择,而Android自带的只有4种可以选择。我使用

了一个工程来测试所有的字体效果。

效果图:

java代码:

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

public class MainActivity extends Activity {

    private int ttf_str[] = { R.string.ttf_1, R.string.ttf_2, R.string.ttf_3,
            R.string.ttf_4, R.string.ttf_5, R.string.ttf_6, R.string.ttf_7,
            R.string.ttf_8, R.string.ttf_9, R.string.ttf_10, R.string.ttf_11,
            R.string.ttf_12, R.string.ttf_13, R.string.ttf_14, R.string.ttf_15,
            R.string.ttf_16, R.string.ttf_17, R.string.ttf_18, R.string.ttf_19, };
    private int ttf_id[] = { R.id.ttf_1, R.id.ttf_2, R.id.ttf_3, R.id.ttf_4,
            R.id.ttf_5, R.id.ttf_6, R.id.ttf_7, R.id.ttf_8, R.id.ttf_9,
            R.id.ttf_10, R.id.ttf_11, R.id.ttf_12, R.id.ttf_13, R.id.ttf_14,
            R.id.ttf_15, R.id.ttf_16, R.id.ttf_17, R.id.ttf_18, R.id.ttf_19, };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /*
         * 设置自定义自体
         */
        for (int i = 0; i <= 18; ++i)
            showTTF("fonts/" + getString(ttf_str[i]), ttf_id[i]);
    }

    private void showTTF(String path, int id) {
        Typeface fontFace = Typeface.createFromAsset(getAssets(), path);
        TextView text = (TextView) findViewById(id);
        text.setTypeface(fontFace);
    }
}

注:Android工程包下载。工程中包含所有的TTF字体。点这里

 

原文地址:https://www.cnblogs.com/orangebook/p/3960736.html