【问题标题】:Custom TextView with Bold, Normal & Italic Styles具有粗体、正常和斜体样式的自定义 TextView
【发布时间】:2017-02-02 14:15:35
【问题描述】:

我正在尝试使用自定义字体系列创建TextView。我创建了一个支持普通字体的。但我无法在自定义 TextView 上实现 bold/italic 样式。

这是我的自定义TextView

public class KTextView extends TextView {

    public KTextView(Context context) {
        super(context);
        TypefaceHelper.setTypeface(context, this);
    }

    public KTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypefaceHelper.setTypeface(context, this);
    }

    public KTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypefaceHelper.setTypeface(context, this);
    }

}

这是TypefaceHelper

public class TypefaceHelper {
private static final String FONT = "AvenirNext-Regular.otf";

private static Typeface typeface = null;

public static void setTypeface(Context context, TextView textView) {
    if (typeface == null) {
        typeface = Typeface.createFromAsset(context.getAssets(), FONT);
    }
    textView.setTypeface(typeface);
}

谁能提出一种实现粗体和斜体样式的方法。

【问题讨论】:

标签: android textview


【解决方案1】:

试试这个:

textview.setTypeface(typeface, Typeface.BOLD);

您也可以使用ITALICBOLD_ITALIC

用于自定义文本视图:

 public class MyTextView extends TextView {

      public MyTextView(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
      }

     public MyTextView(Context context, AttributeSet attrs) {
          super(context, attrs);
      }

     public MyTextView(Context context) {
          super(context);
     }


     public void setTypeface(Typeface tf, int style) {
           if (style == Typeface.BOLD) {
                super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/fonts_name")/*, -1*/);
            } else {
               super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/fonts_name")/*, -1*/);
            }
      }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2016-01-02
    • 1970-01-01
    相关资源
    最近更新 更多