移除TextInputLayout多余的顶部内边距

34

TextInputLayout在顶部似乎总是有一些额外的填充(即使所有边距/填充都设置为0):

输入图像描述

布局看起来像:

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.AppCompatEditText
            android:id="@+id/txt_amount"
            style="@style/EditTextStyle"
            android:hint="@string/hint_amount"
            android:inputType="numberDecimal"/>
    </android.support.design.widget.TextInputLayout>
如何去除这个额外的空格?

1
你是指文本之上还是在View边界之上?https://dev59.com/jJzha4cB1Zd3GeqPIKWQ#40696708 - Mike M.
@MikeM。我的意思是在视图之上(即EditTextTextInputLayout顶部边框之间的空间)。 - 0leg
4
好的,关于浮动提示的问题。如果您不想使用它,您可以在布局XML中使用hintEnabled="false"来禁用它,就像我在链接答案中展示的那样。哦,我猜有人也在这里发布了它。 - Mike M.
1
如何去除两侧的神秘填充? - Sevastyan Savanyuk
9个回答

71

你可以通过将app:hintEnabled="false"设置为TextInputLayout来消除AppCompatEditText上方的额外空格,但在重新启用它之前它不会显示提示。

有关更多信息,请转到Android开发者网站 - TextInputLayout

请查看下面的代码

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:hintEnabled="false">

    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/txt_amount"
        style="@style/EditTextStyle"
        android:hint="@string/hint_amount"
        android:inputType="numberDecimal"/>
</android.support.design.widget.TextInputLayout>
希望这有所帮助。 @Rajesh

1
好的,这看起来很合理。除非有更好的解决方案,否则将接受。 - 0leg

16

对我来说,接受的答案在版本- 1.2.1 上没有起作用,所以我进行了一些实验并意识到填充实际上并不是来自于TextInputLayout,而是来自于TextInputEditText。所以我先从中删除了填充,但它看起来不均匀,因此我为TextInputEditText设置了自定义填充,并且它很好地工作了。只需在TextInputEditText中添加这行代码:android:padding="16dp",将16dp替换为您想要的值即可。


15

具有以下这种结构:

<com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/til_project"
                    style="@style/LoginTextInputLayoutStyle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    app:errorEnabled="false">

                    <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/totalPiecesProduct"
                        android:layout_gravity="center"
                        android:gravity="center"
                        android:padding="0dp"
                        android:textSize="13sp"
                        style="@style/TextInputEditTextStyle"/>

                </com.google.android.material.textfield.TextInputLayout>

关键是:

app:errorEnabled="false"TextInputLayout

`android:padding="0dp"` in `TextInputEditText`

之前:

enter image description here

之后:

enter image description here


如何去除EditText周围的边距? - undefined

2

要删除TextInputLayout中的所有填充,您可以在TextInputLayout中将app:boxCollapsedPaddingTop设置为0dp,并在Edittext中将android:padding设置为0dp。


将Edittext中的android:padding设置为0dp,效果非常好。 - Ucdemir

1

我已经在TextInputEditText中使用了手动设置填充

android:paddingHorizontal="@dimen/_12sdp"
android:paddingVertical="@dimen/_10sdp"

1
你可以通过覆盖TextInputStyle的默认填充样式来实现,(Material版本应该在1.1.0以上)。
 <style name="CustomInputLayoutPadding" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
    <item name="boxBackgroundColor">@color/transparent</item>
    <item name="materialThemeOverlay">@style/CustomOverlayFilledPadding</item>
</style>

那么

 <style name="CustomeOverlayFilledPadding">
    <item name="editTextStyle">@style/CustomTextInputEditPaddingStyle</item>
</style>

那么

   <style name="CustomTextInputEditPaddingStyle" parent="@style/Widget.MaterialComponents.TextInputEditText.FilledBox">
    <item name="android:paddingStart" ns2:ignore="NewApi">2dp</item>
    <item name="android:paddingEnd" ns2:ignore="NewApi">2dp</item>
    <item name="android:paddingLeft">0dp</item>
    <item name="android:paddingRight">2dp</item>
    <item name="android:paddingTop">28dp</item>
    <item name="android:paddingBottom">12dp</item>
</style>

0

compileSdk 33 或 Material library 版本 v1.8.0 开始,我添加了该属性

app:boxBackgroundMode="filled"

<com.google.android.material.textfield.TextInputLayout


-1

你可以使用这个

  <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColorHint="@android:color/white">

            <com.bugle.customviews.RobotoLightEditText

                android:id="@+id/edtName"
                style="@style/StyledTilEditText"
                android:hint="@string/name"
                android:imeOptions="actionNext"
                android:inputType="textCapWords" />

        </android.support.design.widget.TextInputLayout>

-3
将此行添加到EditText中:

android:translationY="-10dp"


3
看起来像是一次黑客攻击。如果有地方可以将其设置为零,尽量避免使用它。 - 0leg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接