简体   繁体   English

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

[英]Custom TextView font not applied on setting text from java android

I have created a Custom TextView to use Font Awesome support, its working fine when you add the text (unicode  ) in layout xml. 我创建了一个Custom TextView来使用Font Awesome支持,当你在layout xml中添加文本(unicode  )时它工作得很好。 But if am trying to set the text from my Adapter dynamically using view.setText() , its not applying the font. 但是,如果我试图使用view.setText()动态设置我的适配器中的文本,它不应用字体。

FontView class FontView类

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 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);
    }
}

Please help whats am doing wrong. 请帮忙我做错了什么。

After struggling more than 3 hrs. 经过3个多小时的挣扎。 I have found the answer here. 我在这里找到了答案。 It was unicode issue. 这是unicode问题。

Changing Adapter setText() to use Html.fromHtml("&#xf179;").toString() fixed the issue 更改适配器setText()以使用Html.fromHtml("&#xf179;").toString()修复了问题

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

Read more here 在这里阅读更多

I hope you guys find it useful. 我希望你们发现它有用。

Could not find an error of your code. 找不到您的代码错误。

This the custom textView which I'm using. 这是我正在使用的自定义textView。 Create font folder inside your assets folder 在assets文件夹中创建字体文件夹

CustomText.java 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);
         }

    }
}

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM