2016-09-19 34 views
2

是否有可能改變TextInputLayout錯誤文本字體爲一個EditText?變化TextInputLayout錯誤字體?

我只能改變顏色或文本大小,通過應用:errorTextAppearance。

+0

請出示你有什麼完全盡力了,那麼你將會很容易找出解決方案。 – Androider

回答

3

可以使用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); 
    } 
} 
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(); 
    } 
}