自定义TextView字体不应用于设置文本从java android



我已经创建了一个自定义TextView来使用字体Awesome支持,当你在布局xml中添加文本(unicode )时,它的工作很好。但是,如果我试图设置文本从我的适配器动态使用view.setText(),它不应用字体。

<<p> FontView类/strong>
public class FontView extends TextView {
    private static final String TAG = FontView.class.getSimpleName();
    //Cache the font load status to improve performance
    private static Typeface font;
    public FontView(Context context) {
        super(context);
        setFont(context);
    }
    public FontView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFont(context);
    }
    public FontView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setFont(context);
    }
    private void setFont(Context context) {
        // prevent exception in Android Studio / ADT interface builder
        if (this.isInEditMode()) {
            return;
        }
        //Check for font is already loaded
        if(font == null) {
            try {
                font = Typeface.createFromAsset(context.getAssets(), "fontawesome-webfont.ttf");
                Log.d(TAG, "Font awesome loaded");
            } catch (RuntimeException e) {
                Log.e(TAG, "Font awesome not loaded");
            }
        }
        //Finally set the font
        setTypeface(font);
    }
}

XML for Layout

 <com.domain.app.FontView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textSize="60sp"
    android:textAlignment="center"
    android:text="&#xf179;"
    android:gravity="center"
    android:id="@+id/iconView"
    android:background="@drawable/oval"
    android:padding="10dp"
    android:layout_margin="10dp" />

My Adapter

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    IconListHolder viewHolder;
    if( convertView == null ) {
        LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = layoutInflater.inflate(R.layout.icon, null);
        viewHolder = new IconListHolder(v);
        v.setTag(viewHolder);
    } else {
        viewHolder = (IconListHolder) v.getTag();
    }
    //Set the text and Icon
    viewHolder.textViewIcon.setText(pages.get(position).getIcon());
    viewHolder.textViewName.setText(pages.get(position).getTitle());
    return v;
}
private class IconListHolder {
    public FontView textViewIcon;
    public TextView textViewName;
    public IconListHolder(View base) {
        textViewIcon = (FontView) base.findViewById(R.id.iconView);
        textViewName = (TextView) base.findViewById(R.id.iconTextView);
    }
}

请帮助我做错了什么

挣扎了3个多小时后。我在这里找到了答案。这是unicode问题。

改变适配器setText()使用Html.fromHtml("&#xf179;").toString()修复问题

viewHolder.textViewIcon
   .setText(Html.fromHtml(pages.get(position).getIcon()).toString());

点击这里阅读更多

无法找到您的代码错误。

这是我使用的自定义textView。在assets文件夹

中创建字体文件夹

CustomText.java

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class CustomText extends TextView {
public CustomText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(attrs);
}
public CustomText(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(attrs);
}
public CustomText(Context context) {
    super(context);
    init(null);
}
private void init(AttributeSet attrs) {
    if (attrs!=null) {
        {
             Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(),"fonts/custom_font.ttf");
             setTypeface(myTypeface);
         }
    }
}
} 

最新更新