问题描述
这会导致TextInputLayout垂直占用过多空间.
疯狂的是,TextInputLayout高度的一半仅用于错误文本(在EditText
的水平线下方).错误文本的字体大小仅为11dp.理想情况下,我想将错误文本顶部和底部的空间减小到最小,例如2dp
It results in the TextInputLayout taking too much space vertically.
It's crazy that half of the TextInputLayout height is just for the error text (below the horizontal line in of EditText
). The font size of the error text is only 11dp. Ideally, I'd like reduce the space on top and bottom of the error text to something very minimal like 2dp
我已经尝试了所有方法,但只设法将TextInputLayout的高度降低到70dp.
I've tried everything but I've only managed to reduced the height of the TextInputLayout to 70dp.
我还有什么可以做的吗?
Is there anything else that I can do?
布局:
<?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>
错误样式:
<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>
推荐答案
虽然这不能完全解决您的问题,但会有所帮助.我也一直在寻找答案,但是没有找到任何解决方案,我的hacky方式涉及启用和禁用TextInputLayout上的错误.它在某种程度上有帮助.
While this does not solve your problem entirely it will help. I too was looking for an answer to this but not finding any solution my hacky way involves enabling and disabling the error on the TextInputLayout. It helps in some way.
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
}
您可以使用这两个功能来设置错误,并在需要时使用文本观察器清除该错误.这并不理想,也无法回答您的问题,但这是针对路人的,这可以帮助他们解决用例.
You can use these two functions to set the error and clear it when needed using a text watcher. Not ideal and does not answer your question but it is for passers by for whom this may help solving their use case.
textwatcher代码,以备不时之需
textwatcher code incase you need it
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) {}
})
使用您的edittext代替电子邮件.希望它可以帮助某人.同样,所有示例都在kotlin中.
In place of email use your edittext. Hope it helps someone.Also all examples are in kotlin.
这篇关于如何缩小TextInputLayout错误文本的填充/边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!