第一步:将第三方字体文件放到assets文件夹下(如:iconfont字体图标)
第二步:重写TextView控件
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
init(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomTextView(Context context, AttributeSet attrs, int defSyle) {
super(context, attrs, defSyle);
init(context);
}
/***
* 设置字体
*
* @return
*/
public void init(Context context) {
setTypeface(FontCustom.setFont(context));
}
}
public class FontCustom {
static String fongUrl = "fonts/iconfont.ttf";
static Typeface tf;
/***
* 设置字体
*
* @return
*/
public static Typeface setFont(Context context) {
if(tf==null){
tf = Typeface.createFromAsset(context.getAssets(), fongUrl);
}
return tf;
}
}
<CustomTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" 字体图标"
android:textColor="@color/login_font_hit"
android:textSize="14.0sp" />
这篇文档对您是否有帮助?