![](/img/trans.png)
[英]Custom TextView font not applied on setting text from java android
[英]Custom font not getting applied
我的自定義字體類
public class CustomFontText extends TextView {
/*
* Caches typefaces based on their file path and name, so that they don't have to be created every time when they are referenced.
*/
private static Typeface mTypeface;
public CustomFontText(final Context context) {
super(context, null);
}
public CustomFontText(final Context context, final AttributeSet attrs) {
super(context, attrs, 0);
readAttrs(context, attrs);
}
public CustomFontText(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
readAttrs(context, attrs);
}
private void readAttrs(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
// Read the title and set it if any
String fontName = a.getString(R.styleable.CustomTextView_fontname);
if (fontName != null) {
// We have a attribute value
if (mTypeface == null) {
mTypeface = Typeface.createFromAsset(context.getAssets(), fontName);
setTypeface(mTypeface);
}
}
// a.recycle();
}
}
在XMl文件中應用
<somepackage.CustomFontText
android:id="@+id/details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ewfewfewqfewfwef"
custom:fontname="Roboto-Regular.ttf" />
它沒有給出任何類型的錯誤,但是我無法在textview中查看任何更改。 更改字體名稱沒有區別。
移動代碼setTypeface(mTypeface);
在檢查mTypeface == null
應該可以解決此問題。 因此,代碼應如下所示:
if (mTypeface == null) {
mTypeface = Typeface.createFromAsset(context.getAssets(), fontName);
}
setTypeface(mTypeface);
這是因為mTypeface
被聲明為static
並且所有CustomFontText
實例都共享同一字體(這對於緩存很有意義)。 但是,如果在檢查中調用setTypeface
則在首次加載字體時,它將僅應用一次。
實際上,我不知道您為什么不工作,但是,也可以使用chrisjenx的Calligraphy 。 我在我的一個項目中使用了它,效果很好!
在項目的資產文件夾中創建文件夾字體 ,而不是僅使用字體名稱作為custom:fontname ,而是使用文件的路徑。
custom:fontname="fonts/Roboto-Regular.ttf"
在您的資產文件夾中添加ttf或otf文件。
使用TextView創建自定義類擴展
public class CustomText extends TextView {
public CustomText (Context context) {
super(context);
createTextView(context, null);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
createTextView(context, attrs);
}
public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
createTextView(context, null);
}
private void createTextView(Context context, AttributeSet attrs) {
String fontName;
TypedArray typedArray;
if (isInEditMode())
return;
if (attrs != null) {
typedArray = context.obtainStyledAttributes(attrs, R.styleable.FontTypeFace, 0, 0);
fontName = typedArray.getString(R.styleable.FontTypeFace_typeface);
setFontTypeFace(context, fontName);
typedArray.recycle();
}
}
private void setFontTypeFace(Context context, String fontName) {
if (fontName != null) {
Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName);
setTypeface(typeface);
}
}
}
在attrs文件中聲明樣式:
<declare-styleable name="FontTypeFace">
<attr name="typeface" format="string" />
</declare-styleable>
使用xml文件中的自定義textview創建控件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal"
android:weightSum="1">
<com.Widget.CustomTextView
android:id="@+id/txt_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/dimen_text_size_12"
app:typeface="@string/thin" />
</LinearLayout>
只需在string.xml文件中添加資產文件名
<!--String for assets font type file name -->
<string name="bold">bold.otf</string>
<string name="light">light.otf</string>
<string name="medium">medium.otf</string>
<string name="regular">regular.otf</string>
<string name="regular_italic">regular_italic.otf</string>
<string name="semi_bold">semibold.otf</string>
<string name="thin">thin.otf</string>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.