TextInputLayout 中分隔线和错误文本之间的填充

Padding between divider line and error text in TextInputLayout

如何在TextInputLayout中设置divider lineerror text(图中标有问号)之间的填充?我尝试在指定为 errorTextAppearance 的样式中添加填充,但没有效果。

编辑:布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="test.textinputlayouttest.MainActivity">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:errorEnabled="true"
        app:errorTextAppearance="@style/Error">

        <EditText
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter address"
            android:inputType="textEmailAddress"
            android:padding="26dp"/>
    </android.support.design.widget.TextInputLayout>

</android.support.constraint.ConstraintLayout>

和风格:

<style name="Error" parent="TextAppearance.AppCompat">
    <item name="android:textColor">@color/red</item>
    <item name="android:paddingBottom">300dp</item>
</style>

试试这个方法..我认为这会起作用..

<android.support.design.widget.TextInputLayout
            android:id="@+id/usernameWrapper"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:padding="26dp"
                android:id="@+id/username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:hint="Username"/>

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

您可以通过

获取错误文本视图
TextView textView = (TextView) inputLayout.findViewById(android.support.design.R.id.textinput_error);

并修改布局参数。

如果您只需要在 divider lineerror text

之间填充

使用前面的例子只是改变 android:padding="26dp" 至 -> android:layout_marginBottom="20dp"

<android.support.design.widget.TextInputLayout
                android:id="@+id/usernameWrapper"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:layout_marginBottom="20dp"
                    android:id="@+id/username"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textEmailAddress"
                    android:hint="Username"/>

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

在内部 EditText

中使用 android:layout_marginBottom="-10dp"
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.textfield.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/textInputLayout"
    style="@style/Widget.Design.TextInputLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    >
<com.google.android.material.textfield.TextInputEditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="-10dp"
    tools:text="Text"
    />
</com.google.android.material.textfield.TextInputLayout>

适用于以下版本:com.google.android.material:material:1.5.0