如何减少文本提示和文本行之间的填充?

How do I reduce the padding between the text hint and text line?

我有一个带有提示的 TextInputEditText。我真的不喜欢提示和文本行之间的 space 数量。我试过将重力设置到底部但没有运气。

       <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="400dp"
                    android:layout_height="wrap_content"
                    android:background="@android:color/white"
                    android:inputType="textNoSuggestions"
                    android:hint="@string/email_hint"
                    android:textColorHint="@color/color_hint_grey"
                    android:fontFamily="@font/alternate_got_no1d"
                    android:textSize="20sp"/>

为了减少文本框的高度,您可以使用 dense style,这将减少文本框内的垂直填充

<com.google.android.material.textfield.TextInputLayout
       ....
       android:hint="Hint text"       
       style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense" >

否则您可以使用以下方式定义(需要 1.1.0 版)自定义样式:

  <style name="MyDense" parent="Widget.MaterialComponents.TextInputLayout.FilledBox.Dense">
    <item name="materialThemeOverlay">@style/MyThemeOverlayFilledDense</item>
  </style>

  <style name="MyThemeOverlayFilledDense">
    <item name="editTextStyle">@style/MyTextInputEditText_filledBox_dense</item>
  </style>

  <style name="MyTextInputEditText_filledBox_dense" parent="@style/Widget.MaterialComponents.TextInputEditText.FilledBox.Dense">
    <item name="android:paddingTop">24dp</item>
    <item name="android:paddingBottom">4dp</item>
  </style>

这里是结果(使用默认样式和自定义样式):

将此添加到您的 com.google.android.material.textfield.TextInputEditText:

                android:paddingStart="0dp"
                android:paddingLeft="0dp"
                android:paddingRight="0dp"
                android:paddingEnd="0dp"

要删除所有方向的填充:

注意:不推荐使用 paddingTop="0dp",因为插入的文本会覆盖标题,所以我会尝试使用其他属性。

android:paddingStart="0dp"
android:paddingEnd="0dp"
android:paddingTop="0dp" 
android:paddingBottom="0dp"

根据您的填充需要更改 dp 值。 请记住,如果您使用 paddingLeft 和 paddingRight,它在具有 rtl(从右到左)语言设置(如阿拉伯语和希伯来语)的设备中的外观会很糟糕。