【问题标题】:Custom TextView font not applied on setting text from java android自定义 TextView 字体不适用于从 java android 设置文本
【发布时间】:2015-07-28 18:22:29
【问题描述】:

我创建了一个自定义 TextView 以使用 Font Awesome 支持,当您在布局 xml 中添加文本(unicode )时,它可以正常工作。但是,如果我尝试使用 view.setText() 动态设置来自我的适配器的文本,则它不会应用字体。

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

 <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" />

我的适配器

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

请帮忙看看哪里出错了。

【问题讨论】:

  • 如何创建动态使用的文本?从数据库? XML 字符串?硬编码?我们需要更多信息,在目前的状态下,这个问题是无法解决的。
  • @Rolfツ 已经解决了,看我的回答。文本来自 JSON API 作为字符串

标签: java android textview android-adapter font-awesome-4


【解决方案1】:

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

将适配器 setText() 更改为使用 Html.fromHtml("&amp;#xf179;").toString() 修复了问题

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

Read more here

希望对大家有用。

【讨论】:

    【解决方案2】:

    找不到您的代码错误。

    这是我正在使用的自定义 textView。在资产文件夹中创建字体文件夹

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

    【讨论】:

    • 是的,它的工作原理?问题是当我动态设置文本时它不应用字体
    • @Saqueib 是有线的。您是否尝试将字体设置为 textview 而不是创建 customview 而是使用 textView 中的 setTypface() 方法?
    • 已经做了,同样的问题。当您在布局 xml android:text="&amp;#xf179;" 中对字符串进行硬编码时,字体甚至正在加载它的工作
    • @Saqueib 当您使用 java 代码设置文本时,只需检查手动键入的字符串而不是 pages.get(position).getIcon()。
    • 测试了viewHolder.textViewIcon.setText("&amp;#xf179;");没有运气
    猜你喜欢
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多