2016-09-19 6 views
2

EditTextのTextInputLayoutエラーテキストフォントを変更することはできますか?TextInputLayoutのエラーフォントを変更しますか?

app:errorTextAppearanceを使用して、色またはテキストサイズを変更することしかできませんでした。

あなたは、フォント設定する SpannableStringを使用することができます
+0

あなたは何を完全に試してみたかを示してください。それで解決策を見つけるのは簡単でしょう。 – Androider

答えて

3

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

別の方法あなたがそれを好きならば、あなたはを設定することができます。特定のTypefaceセットを持ってい

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

カスタムSpanクラスをエラーの色またはフォント両方

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(); 
    } 
} 
関連する問題