Android应用开发使用第三方字体

Android原生的字体可能不会让UI妹纸欢心,实在觉得它太丑了,于是乎UI妹纸就用了第三方字体作为APP的字体风格,这篇博客就是总结在Android应用开发中怎样使用第三方字体。

         首先得有第三方字体库,这里的字体库文件是black_simplified.TTF,在Android Assert目录下新建front文件夹,并将字体库文件放在front目录下面,即/Assert/front/black_simplified.TTF 

         这里来总结下怎样在应用中使用第三方字体才是最简便的。以TextView为例,API接口中有一个方法叫做setTypeface(Typeface obj),这就是设置TextView显示的字体样式的接口,那么怎样得到Typeface对象呢,查看API后可以由Typeface.creatFromAssert(obj)方式来获取,单个TextView使用第三方字体的整个流程如下:

public class MainActivity extends Activity {
	private Typeface TEXT_TYPE ;
	private TextView mTv ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 加载自定义字体
		try{
			TEXT_TYPE = Typeface.createFromAsset(getAssets(),"fronts/black_simplified.TTF"); 
		}catch(Exception e){
			Log.i("MainActivity","加载第三方字体失败。") ;
			TEXT_TYPE = null ;
		}
		mTv = (TextView)findViewById(R.id.tv1) ;
		if(TEXT_TYPE != null){
			mTv.setTypeface(TEXT_TYPE) ;
		}
		mTv.setText("Android程序开发") ;
	}
}

运行之后即可看到效果。但是这样是不是很麻烦?!要为每一个需要用到第三方字体的控件设置字体样式。这里的解决办法是重写需要使用第三方字体的控件,这里以TextView为例,当然Android中可以显示字体的控件还有很多,如Button、EditText。Android开发中常常需要实现自己的Application对象,因为它是全局的,也是程序最先执行的模块,也便于数据共享,所以,初始化字体的操作就放在我们自定义的Application子类MyApp中。代码片段如下:

在MyApp.java的onCreate函数中初始化自定义的typeface:

@Override
	public void onCreate() {
		// 加载自定义字体
		try{
			TEXT_TYPE = Typeface.createFromAsset(getAssets(),"fronts/black_simplified.TTF"); 
		}catch(Exception e){
			Log.i("MyApp","加载第三方字体失败。") ;
			TEXT_TYPE = null ;
		}
		super.onCreate();
	}

在AndroidManifest.xml文件中的<application>中注明name属性为自定义的Application子类:

<application
        android:name="com.example.androidfronttypeface.MyApp"
        ……
</application>

自定义TextView:

public class MyFrontTextView extends TextView {
	public MyFrontTextView(Context context) {
		super(context);
		setTypeface() ;
	}
	public MyFrontTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setTypeface() ;
	}
	public MyFrontTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setTypeface() ;
	}
	private void setTypeface(){
		// 如果自定义typeface初始化失败,就用原生的typeface
		if(MyApp.TEXT_TYPE == null){
			setTypeface(getTypeface()) ;
		}else{
			setTypeface(MyApp.TEXT_TYPE) ;
		}
	}
}
Layout文件中需要引用自定义的TextView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.example.androidfronttypeface.MyFrontTextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=”Android应用开发”
 />
</RelativeLayout>

大概就是这样的,如果是Button或其他控件需要使用第三方字体,也是同样的道理。

源码参考








  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值