Andorid第三方字体库导入
一、问题提出
最近在进行项目设计时,想使用其他一些字体,但android自带的字体种类好像就是那么几种,查阅一些资料,整理了一下android几种导入第三方字体库的方法。权当作为笔记的整理了。
二、解决方法
1.自定义控件
详见我的另一篇文章
【Andorid自定义控件之TextView自定义字体】(https://blog.****.net/jsc1996/article/details/89609424)
2.使用第三方字体导入框架
这里推荐一个第三方全局导入字体的项目
Calligraphy
(1).首先点击下载.aar第三方库,并进行相关配置。
(2)加载本地第三方字体
将自定义字体添加到assets /目录下,字体导入和自定义控件中的方法一致。如图:
(3).安装
1.新建Application
package com.example.recordmemory;
import android.app.Application;
import android.content.Context;
import uk.co.chrisjenx.calligraphy.CalligraphyConfig;
public class BaseApplication extends Application {
private static Context context;
@Override
public void onCreate() {
super.onCreate();
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/Helvetica.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
context = getApplicationContext();
}
public static Context getContext(){
return context;
}
2.在Activity中重写attachBaseContext方法。
public class BaseActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
}
(3).使用
fontPath="fonts/FZWBJ.TTF"
<EditText
fontPath="fonts/FZWBJ.TTF"
android:id="@+id/edit_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
style="@style/EditTextTitleStyle"
tools:ignore="MissingPrefix"
android:background="@null"/>