Android输入框文字不垂直居中
最近升级了项目tagetSdk版本,发现所有EditView文字都不居中了,如下
输入区文字不在光标中间,没有垂直居中,无论怎么调整padding都不行。后来查源码,发现官方为了解决不同语言输入出现的抖动问题,默认启用了本地配置行高作为最小行高。
<!-- Whether to use the locale preferred line height for the minimum line height.This flag is useful for preventing jitter of entering letters into empty EditText.The line height of the text is determined by the font files used for drawing text in aline. However, in case of the empty text case, the line height cannot be determined andthe default line height: usually it is came from a font of Latin script. By making thisattribute to true, the TextView/EditText uses a line height that is likely used for thelocale associated with the widget. For example, if the system locale is Japanese, theheight of the EditText will be adjusted to meet the height of the Japanese font even ifthe text is empty.The default value for EditText is true if targetSdkVersion is{@link android.os.Build.VERSION_CODE#VANILLA_ICE_CREAM} or later, otherwise false.For other TextViews, the default value is false. --> <!-- @FlaggedApi("com.android.text.flags.fix_line_height_for_locale") --> <attr name="useLocalePreferredLineHeightForMinimum" format="boolean" />
只需要在EditText上添加如下属性即可自定义行高了。
<AppCompatEditText ......android:useLocalePreferredLineHeightForMinimum="false"......
/>