Android输入框文字不垂直居中

发布于:2025-08-19 ⋅ 阅读:(18) ⋅ 点赞:(0)

最近升级了项目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 a
  line. However, in case of the empty text case, the line height cannot be determined and
  the default line height: usually it is came from a font of Latin script. By making this
  attribute to true, the TextView/EditText uses a line height that is likely used for the
  locale associated with the widget. For example, if the system locale is Japanese, the
  height of the EditText will be adjusted to meet the height of the Japanese font even if
  the 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"
    ......
/>

网站公告

今日签到

点亮在社区的每一天
去签到