TextInputLayout错误文本填充

8
我正在使用具有自定义XML形状背景的AppCompatEditTextTextInputLayout。每当我设置一些错误时,错误行始于布局的开头。是否有任何方法给该错误行添加填充?

enter image description here


请参考以下链接:https://dev59.com/11wY5IYBdhLWcg3wcXeE - sasikumar
你能否更新一下这个问题,并附上你尝试过的代码? - Kae10
使用 setError(null) - BST Kaal
将背景图像从EditText中移除,这样就可以解决背景图像对EditText造成的问题。 - Mahesh Suthar
3个回答

6

您可以通过以下方式引用错误的TextView:

TextView textView = (TextView) inputLayout.findViewById(android.support.design.R.id
            .textinput_error);

然后你可以获取它的布局参数进行操作。

1
冒险的黑客行为很危险。但同时,我认为ID不会很快改变。 - Aba
请记住,错误文本视图仅在显示错误时创建,因此您需要在TextInputLayout中显示错误后更改参数。 - Almighty

2

我曾经在这个问题上苦苦挣扎,下面是我的解决方法:

移除错误文本视图的父级父级元素的填充

使用这个解决方案,我找到了错误文本视图(因为我需要向它添加一个图标),但无法去掉填充,所以试了几次后,我意识到TextInputLayout有两个子元素,从第二个子元素中移除填充就可以解决问题。 为了确保,我也将其从第一个子元素中移除了

利用Kotlin扩展,我提出了一个简洁的解决方案

/**
 * TextInputLayout
 */
fun TextInputLayout.removePadding() {
    for (i in 0 until this.childCount) {
        this.getChildAt(i).setPadding(0)
    }
}

您也可以使用相同的逻辑在Java中实现。

enter image description here


0
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
    android:id="@+id/tx_in_username"
    android:errorEnabled="true"
    android:layout_marginLeft="@dimen/margin_LEFT_RIGHT"
    android:layout_marginRight="@dimen/margin_LEFT_RIGHT"
android:layout_height="wrap_content">
<EditText
    android:layout_width="match_parent"
    android:layout_marginTop="10dp"
    android:layout_height="36sp"
    android:hint="Username"
    />


对于内部的EditText,请不要使用marginLeft和marginRight。 - Mallikarjuna

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