Change font of the floating label EditText and TextInputLayout

前端 未结 12 1081
北海茫月
北海茫月 2020-12-03 01:23

Someone tried to change the font of the floating label? I changed the source of EditText but the font of the floating label did not change, I am very grateful to those who h

12条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-03 01:33

    fixing a problem in @adneal answer: if setErrorEnabled is not set true, mErrorView would be null and if you set it false at any point the font would change back to default. so to fix it:

    in you custom TextInputLayout override setErrorEnabled

    @Override
    public void setErrorEnabled(boolean enabled) {
    
        super.setErrorEnabled(enabled);
    
        if (enabled) {
    
            try {
    
                Field cthf = TextInputLayout.class.getDeclaredField("mErrorView");
                cthf.setAccessible(true);
    
                TextView error = (TextView) cthf.get(this);
    
                if (error != null)
                    error.setTypeface(tf);
    
    
            } catch (Exception e) {
    
            }
        }
    }
    

提交回复
热议问题