Android下EditText中的字体不统一问题
好久沒寫,今天心情好略記下解決的某bug
在一個登錄界面有帳號和密碼兩個EditText,但是卻發(fā)現(xiàn)兩個EditText的hint的英文字體不同,看著極不協(xié)調。但是兩個EditText都沒有特意設置過hint的字體啊···
經(jīng)實驗,把密碼欄的android:password="true"屬性去掉后字體變一樣。應該是這個屬性的影響。
經(jīng)查,hint字體受EditText的text字體影響,即受android:typeface的影響。android:typeface有三個取值:“sans”,“serif”,“monospace”,而缺省的話普通EditText是Sans,想必密碼框的缺省不同的原因。然后將兩個EditText設置相同typeface,但是發(fā)現(xiàn)能正常改變帳號欄的hint字體,但是密碼欄hint字體一直保持是monospace不變。
果斷看源碼,發(fā)現(xiàn)在EditText的初始化中有這么一段:
if (password) {
??? setTransformationMethod(PasswordTransformationMethod.getInstance());
??? typefaceIndex = MONOSPACE;
}
果然密碼框初始化會強制設置字體為MONOSPACE,這樣解決就簡單了,在代碼實現(xiàn)中加上:
idEdit.setTypeface(Typeface.SANS_SERIF);
passEdit.setTypeface(Typeface.SANS_SERIF);
這樣字體就和諧了~
EditText password = (EditText) findViewById(R.id.register_password_text);
password.setTypeface(Typeface.DEFAULT);
password.setTransformationMethod(new PasswordTransformationMethod());
此外需要去除xml中的android:password="true"語句
總結
以上是生活随笔為你收集整理的Android下EditText中的字体不统一问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android:windowSoftIn
- 下一篇: android eclipse中导入fr