在main文件夹下,新建assets/fonts文件,添加.otf文件
字体工具类
import android.app.Application;
import android.graphics.Typeface;
/**
* 字体工具类
* author:wiki on 2018/11/9
* email:zhengweiqunemail@qq.com
*/
public class CustomFontManager {
private static CustomFontManager singleton;
private Typeface lightFont;
private Typeface boldFont;
private CustomFontManager() {
}
public void init(final Application context) {
//读取字体文件
this.lightFont = Typeface.createFromAsset(context.getAssets(), "fonts/SFCompactText-Light.otf");
this.boldFont = Typeface.createFromAsset(context.getAssets(), "fonts/SFCompactText-Semibold.otf");
}
public synchronized static CustomFontManager getInstance() {
if (singleton == null) {
singleton = new CustomFontManager();
}
return singleton;
}
public Typeface getLightFont() {
return this.lightFont;
}
public Typeface getBoldFont() {
return this.boldFont;
}
}
自定义字体基类
import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
/**
* 自定义字体类型 基类
* author:wiki on 2018/11/9
* email:zhengweiqunemail@qq.com
*/
public abstract class BaseCustomTextView extends AppCompatTextView {
public BaseCustomTextView(Context context) {
super(context);
setFont();
}
public BaseCustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setFont();
}
public BaseCustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setFont();
}
protected abstract void setFont();
}
字体类
import android.content.Context;
import android.util.AttributeSet;
import com.sugrsugr.ivyapp.sugrsmartivy.util.CustomFontManager;
/**
* SFCompactText-Semibold 字体
* author:wiki on 2018/11/9
* email:zhengweiqunemail@qq.com
*/
public class CustomBoldTextView extends BaseCustomTextView {
public CustomBoldTextView(Context context) {
super(context);
}
public CustomBoldTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomBoldTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void setFont() {
setTypeface(CustomFontManager.getInstance().getBoldFont());
}
}
AppManager中初始化字体
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.multidex.MultiDexApplication;
import com.sugrsugr.ivyapp.sugrsmartivy.util.CustomFontManager;
/**
* 初始化资源
* author:wiki on 2018/11/9
* email:zhengweiqunemail@qq.com
*/
public final class AppManager extends MultiDexApplication {
private static AppManager instance;
public static AppManager getInstance() {
return instance;
}
public static Context getContext() {
return instance.getApplicationContext();
}
@Override
public void onCreate() {
super.onCreate();
instance = this;
//初始化字体类
CustomFontManager.getInstance().init(this);
}
/**
* 获取本地软件版本号
*/
public static int getLocalVersion(Context ctx) {
int localVersion = 0;
try {
PackageInfo packageInfo = ctx.getApplicationContext()
.getPackageManager()
.getPackageInfo(ctx.getPackageName(), 0);
localVersion = packageInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return localVersion;
}
/**
* 获取本地软件版本号名称
*/
public static String getLocalVersionName(Context ctx) {
String localVersion = "";
try {
PackageInfo packageInfo = ctx.getApplicationContext()
.getPackageManager()
.getPackageInfo(ctx.getPackageName(), 0);
localVersion = packageInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return localVersion;
}
}
布局文件中使用
<com.sugrsugr.ivyapp.sugrsmartivy.widget.CustomBoldTextView
android:layout_marginTop="27dp"
android:layout_marginBottom="16dp"
android:textColor="@color/green_8CC63F"
android:textSize="25sp"
tools:text="Hi" />