随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)
转自:http://blog.csdn.net/chenzheng_java/article/details/6233849
这里粘贴一个activity的代码,其中展示了Typeface的具体用法。
[java] view plain copy
- package cn.com.chenzheng_java;
- import <a href="http://lib.csdn.net/base/15" class='replace_word' title="Android知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Android</a>.app.Activity;
- import android.graphics.Typeface;
- import android.os.Bundle;
- import android.widget.TextView;
- /**
- * @description 有关字体设置
- * @author chenzheng_java
- *
- */
- public class FontFaceActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.fontface);
- TextView textView = (TextView)findViewById(R.id.textView_fontface);
- /**
- * setTypeface(Typeface tf)设置字体
- * setTypeface(Typeface tf, int style)设置字体,但是其中依旧还有一些倾斜度或者加粗等设置,则通过style进行设置
- */
- /*textView.setTypeface(Typeface.DEFAULT_BOLD,Typeface.ITALIC);
- textView.setText("中国人!");*/
- Typeface tf = Typeface.createFromAsset(getAssets(), "font/ARIAL.TTF");
- textView.setTypeface(tf,Typeface.BOLD_ITALIC);
- textView.setText("中国人!字体设置则样!");
- }
- }
-------------------------------------------------------------------------------
android.graphics.Typeface类指定了字体的style属性,该类可以帮助我们再绘制字体的时候设置字体的具体属性,例如like textSize, textSkewX, textScaleX 等。
创建Typeface的方法有如下几个:
我们代码中使用的为createFromAsset()方法,该方法可以获取我们应用中的asset文件夹下的ttf格式的字体文件,并且根据该文件返回一个Typeface对象。如图
这里一定要注意,path并不是以/开头的,笔者测试的时候呗困扰了很久!如果系统并不能使用我们的字体,那么会自动使用默认字体代替。
这里系统为我们提供了一些默认的字体,如图
因为很多字体并不支持BOLD/ITALIC等样式,所以我们有的时候需要显示的指定,这也是很多create方法中style的作用。
四种取值,为BOLD/ITALIC/NORMAL/BOLD_ITALIC
我们可以通过下面的几个方法对当前typeface的style进行判断: