ITeye博客

自定义字体

gryphone · 2010-05-15 10:49 · anddev · 移动开发 · Windows · XML · Android · Microsoft · Markdown

自定义字体:

 

 

[]代码 步骤]

 

1. 定义包含1 TextView 布局:main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
	android:id="@+id/text"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center"
    />
</LinearLayout>

 

 

 

2. 下载字体文件 后缀名一般为:*.ttf 如:本例为:biscuit_tin.ttf

 

* 在工程目录下的 assets 目录下新建文件夹:fonts 然后把*.ttf文件 复制至该目录 并刷新工程

 

* 代码

 

public class MyTextTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Typeface fontFace = Typeface.createFromAsset(getAssets(), "fonts/biscuit_tin.ttf");
        
        TextView text = (TextView)findViewById(R.id.text);
        text.setTypeface(fontFace);
        text.setText("Hello World!");
        text.setTextSize(100);
    }
}

 

 

 

考虑版权问题 *.ttf 字体文件 要大家自己找了 网络上有很多 我一直在找 微软雅黑  可惜失败 题外话 microsoft-vista 的贡献也仅于此了....

 

 

 

emulator 运行截图: text="Hello World!"

 

评论(3)

  1. #3 楼 · wanday · 2010-05-26 13:48

    在windows里font文件夹里有很多ttf的字体,随便用啊

  2. #2 楼 · zhuixinjian · 2010-05-16 10:55

    楼主能把这个字体共享下不.这个字体有版权没

  3. #1 楼 · syluke · 2010-05-15 13:28

    非常实用的技巧,多谢分享!

评论功能维护中,当前仅展示历史评论。