【问题标题】:Change TextInputLayout error font?更改 TextInputLayout 错误字体?
【发布时间】:2016-09-19 11:57:37
【问题描述】:

是否可以更改 EditText 的 TextInputLayout 错误文本字体?

我只能通过 app:errorTextAppearance 更改颜色或文本大小。

【问题讨论】:

  • 请完整展示您尝试过的方法,然后很容易找到解决方案。

标签: android fonts android-textinputlayout


【解决方案1】:

您可以使用SpannableString 来设置字体:

SpannableString s = new SpannableString(errorString);
s.setSpan(new TypefaceSpan(font), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mPasswordView.setError(s);

具有特定 Typeface 集的自定义 Span 类:

public class TypefaceSpan extends MetricAffectingSpan {
    private Typeface mTypeface;
    public TypefaceSpan(Typeface typeface) {
        mTypeface = typeface;
    }

    @Override
    public void updateMeasureState(TextPaint p) {
        p.setTypeface(mTypeface);
        p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }

    @Override
    public void updateDrawState(TextPaint tp) {
        tp.setTypeface(mTypeface);
        tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }
}

【讨论】:

    【解决方案2】:

    如果你喜欢另一种方法,你可以设置错误颜色或字体两者

    public static void setErrorTextColor(TextInputLayout textInputLayout, int color, Typeface font) {
      try {
        Field fErrorView = TextInputLayout.class.getDeclaredField("mErrorView");
        fErrorView.setAccessible(true);
        TextView mErrorView = (TextView) fErrorView.get(textInputLayout);
        Field fCurTextColor = TextView.class.getDeclaredField("mCurTextColor");
        fCurTextColor.setAccessible(true);
        fCurTextColor.set(mErrorView, color);
        mErrorView.setTypeface(font);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-24
      • 2017-06-25
      • 2017-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      相关资源
      最近更新 更多