我常常遇到很多问题'我的片段中的自定义字体,所以我决定从本教程中创建所有内容:
https://futurestud.io/tutorials/custom-fonts-on-android-extending-textview
它不起作用。我没有收到任何错误。请帮帮我!
答案 0 :(得分:1)
您可以使用 适用于Android的智能字体是一个库,可以在应用程序中轻松使用自定义字体 https://github.com/smart-fun/SmartFonts
答案 1 :(得分:0)
public class LoveTextView extends TextView {
private static final String TAG = "TextView";
public LoveTextView(Context context) {
super(context);
}
public LoveTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public LoveTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.LoveTextView);
String customFont = a.getString(R.styleable.LoveTextView_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: " + e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
than you can use textview class in xml file . like below
<com.love.ui.LoveTextView
android:id="@+id/toolbar_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|center"
android:text="@string/add_product"
android:textColor="@color/title_text_color"
android:textSize="@dimen/material_medium_one"
app:customFont="fonts/Roboto-Regular.ttf" />