您可以通过以下方式引用错误的TextView:
TextView textView = (TextView) inputLayout.findViewById(android.support.design.R.id
.textinput_error);
我曾经在这个问题上苦苦挣扎,下面是我的解决方法:
移除错误文本视图的父级父级元素的填充
使用这个解决方案,我找到了错误文本视图(因为我需要向它添加一个图标),但无法去掉填充,所以试了几次后,我意识到TextInputLayout有两个子元素,从第二个子元素中移除填充就可以解决问题。 为了确保,我也将其从第一个子元素中移除了
利用Kotlin扩展,我提出了一个简洁的解决方案
/**
* TextInputLayout
*/
fun TextInputLayout.removePadding() {
for (i in 0 until this.childCount) {
this.getChildAt(i).setPadding(0)
}
}
您也可以使用相同的逻辑在Java中实现。
<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"
/>
setError(null)
- BST Kaal