更改TextInputLayout错误字体?

时间:2016-09-19 11:57:38

标签: android fonts android-textinputlayout

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

我只能通过app:errorTextAppearance。

更改颜色或文字大小

2 个答案:

答案 0 :(得分:5)

您可以使用SpannableString设置字体:

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

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

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

答案 1 :(得分:0)

如果您喜欢它,可以设置错误颜色或字体

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