如何缩小TextInputLayout错误文本的填充/边距顶部和底部?

eri*_*icn 7 android android-appcompat

它导致TextInputLayout垂直占用太多空间.
令人发疯的是,TextInputLayout高度的一半仅用于错误文本(在水平线下方EditText).错误文本的字体大小仅为11dp.理想情况下,我希望将错误文本顶部和底部的空间缩小到像2dp这样的极小

我已经尝试了一切,但我只是设法将TextInputLayout的高度降低到70dp.

还有什么我可以做的吗?

在此输入图像描述

布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TextInputLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="@dimen/height_textinputlayout"
    android:padding="0dp"
    app:errorTextAppearance="@style/ErrorText">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="@dimen/height_edittext"
        android:layout_marginBottom="0dp"
        android:layout_marginEnd="22dp"
        android:layout_marginStart="22dp"
        android:inputType="textEmailAddress"
        android:maxLines="1"
        android:singleLine="true"/>

</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

错误风格:

<style name="ErrorText" parent="TextAppearance.AppCompat">
        <item name="android:layout_margin">0dp</item>
        <item name="android:padding">0dp</item>
        <item name="android:textColor">@android:color/holo_red_dark</item>
        <item name="android:textSize">@dimen/font_size_form_field_error</item>
        <item name="android:gravity">top</item>
        <item name="fontPath">fonts/Avalon-Book.ttf</item>
        <item name="android:background">@android:color/holo_orange_dark</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

his*_*eed 1

虽然这不能完全解决您的问题,但会有所帮助。我也在寻找这个问题的答案,但没有找到任何解决方案,我的黑客方式涉及启用和禁用 TextInputLayout 上的错误。它在某种程度上有帮助。

fun clearError(til:TextInputLayout,text:Editable?,lengthCheck:Int? = null){
        if(text != null && text.isNotEmpty()){
            if(lengthCheck == null) {
                til.error = null
                til.isErrorEnabled = false
            } else if(text.length >= lengthCheck) {
                til.error = null
                til.isErrorEnabled = false
            }
        }
    }

    fun setErrorTil(til:TextInputLayout,error:String){
        if(!til.isErrorEnabled) til.isErrorEnabled = true
        til.error = error
    }
Run Code Online (Sandbox Code Playgroud)

您可以使用这两个函数来设置错误,并在需要时使用文本观察器清除它。不理想,不能回答您的问题,但它适用于路人,这可能有助于解决他们的用例。

文本观察器代码,以防您需要它

email.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {
            clearError(enameTip,s)}
        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
    })
Run Code Online (Sandbox Code Playgroud)

使用您的编辑文本代替电子邮件。希望它能帮助某人。此外,所有示例均采用 kotlin 语言。