Change TextInputLayout error font?

拟墨画扇 提交于 2019-12-10 16:06:19

问题


Is it possible to change the TextInputLayout error text font for an EditText?

I could only change the color or text size, via app:errorTextAppearance.


回答1:


You can use a SpannableString to set the font:

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

A custom Span class that has a specific Typeface set:

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:


Another method if you like it, you can set Error color or Font both

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


来源:https://stackoverflow.com/questions/39572621/change-textinputlayout-error-font

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!