TextInputLayout 的错误消息文本被截断
Error message text for TextInputLayout is cut off
嘿,有人知道为什么会这样(见图片)吗?
小米 MiA1 会出现这种情况,而在诺基亚 7.1 上工作正常。
我的xml视图布局
FrameLayout - root
ScrollView
RelativeLayout
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/til_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/profile.EMAIL"
android:theme="@style/TextInputLayoutTheme"
android:margin="16dp"
android:paddingBottom="8dp"
app:layout_constraintTop_toBottomOf="@+id/phone_wrapper"
>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:textSize="16sp"
android:textColor="@color/darkTextColor"
android:imeOptions="actionDone"
tools:text="Email"
/>
</com.google.android.material.textfield.TextInputLayout>
我认为放置 TextInputLayoutTheme 在这里不相关,因为我只使用视图的颜色进行操作
我通过将错误字体的大小增加到 14sp(原来是 12sp)解决了这个问题
<com.google.android.material.textfield.TextInputLayout
[...]
app:errorTextAppearance="@style/MyProfile.Error">
<style name="MyProfile.Error" parent="TextAppearance.Design.Error">
<item name="android:textSize">14sp</item>
</style>
尽管正如 Shabbir Dhangot 所说,我可能认为设置 TIL.setErrorEnabled(true) 可能对其他人有所帮助。
Layout Inspector 显示,即使存在一些 space 错误,它也不会在所有错误中绘制视图。 LinearLayouts(和一个简单的 FrameLayout)中的所有内容,所以不可能有任何东西与它重叠,paddings 设置为 0,没有代码更改高度或实现中有可疑之处,所以我不知道什么可能是这种行为的原因。
虽然我找到了一些有效的解决方案。您需要在 TextInputLayout
中找到一个持有错误的视图(ID 为 textinput_error
)并向其添加一些小填充。我通过扩展 TextInputLayout 并将此类代码添加到 init 中来完成。
val errorTV = findViewById<TextView>(R.id.textinput_error)
errorTV.updatePadding(top = 4)
问题跟踪器已经存在问题 - https://issuetracker.google.com/issues/116747167 所以希望 Google 有一天能解决它。
我知道这不是最佳解决方案,但您可以在错误消息的开头添加 \n
。
例如,如果您的错误是Error
,只需将其更改为\nError
。
如果您想更改 \n
的大小,您可以使用:
String newLine = "\n";
String errorMessage = "Error";
SpannableString spannableErrorMessage = new SpannableString(newLine + errorMessage );
spannableName.setSpan(new TextAppearanceSpan(activity, R.style.my_style),0,a.length(), 0);
spannableName.setSpan(new RelativeSizeSpan(2.0f), a.length(), a.length() + 1, 0);
如果您多次在 TextInputLayout 上设置错误,错误消息文本会被截断。
所以每次设置 TextInputLayout 错误消息文本时,只需先将其设置为 null 即可。
textInputLayout.setError(空);
textInputLayout.setError启用(假);<br>
textInputLayout.setError("Error Message");
我通过保存和恢复 LayoutParams 解决了问题:
final ViewGroup.LayoutParams params = textInputLayout.getLayoutParams();
textInputLayout.setError(errorMessage);
textInputLayout.setLayoutParams(params);
正如一些人已经指出的那样,这是一个已知错误。参见 https://issuetracker.google.com/issues/116747167
我通过这样做解决了这个问题(注意下面的代码是 Kotlin,所以 != 适用于字符串):
if(myInputLayout.error != error){
myInputLayout.error = error
}
您可以使用这个 Kotlin 扩展:
/**
* Fix a bug with [TextInputLayout] that cut the error text when setting the same text twice.
*/
fun TextInputLayout.setFixedError(errorTxt: CharSequence?) {
if (error != errorTxt) {
error = errorTxt
}
}
用法:
yourTextInput.setFixedError("Your error text")
嘿,有人知道为什么会这样(见图片)吗? 小米 MiA1 会出现这种情况,而在诺基亚 7.1 上工作正常。
我的xml视图布局
FrameLayout - root
ScrollView
RelativeLayout
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/til_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/profile.EMAIL"
android:theme="@style/TextInputLayoutTheme"
android:margin="16dp"
android:paddingBottom="8dp"
app:layout_constraintTop_toBottomOf="@+id/phone_wrapper"
>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:textSize="16sp"
android:textColor="@color/darkTextColor"
android:imeOptions="actionDone"
tools:text="Email"
/>
</com.google.android.material.textfield.TextInputLayout>
我认为放置 TextInputLayoutTheme 在这里不相关,因为我只使用视图的颜色进行操作
我通过将错误字体的大小增加到 14sp(原来是 12sp)解决了这个问题
<com.google.android.material.textfield.TextInputLayout
[...]
app:errorTextAppearance="@style/MyProfile.Error">
<style name="MyProfile.Error" parent="TextAppearance.Design.Error">
<item name="android:textSize">14sp</item>
</style>
尽管正如 Shabbir Dhangot 所说,我可能认为设置 TIL.setErrorEnabled(true) 可能对其他人有所帮助。
Layout Inspector 显示,即使存在一些 space 错误,它也不会在所有错误中绘制视图。 LinearLayouts(和一个简单的 FrameLayout)中的所有内容,所以不可能有任何东西与它重叠,paddings 设置为 0,没有代码更改高度或实现中有可疑之处,所以我不知道什么可能是这种行为的原因。
虽然我找到了一些有效的解决方案。您需要在 TextInputLayout
中找到一个持有错误的视图(ID 为 textinput_error
)并向其添加一些小填充。我通过扩展 TextInputLayout 并将此类代码添加到 init 中来完成。
val errorTV = findViewById<TextView>(R.id.textinput_error)
errorTV.updatePadding(top = 4)
问题跟踪器已经存在问题 - https://issuetracker.google.com/issues/116747167 所以希望 Google 有一天能解决它。
我知道这不是最佳解决方案,但您可以在错误消息的开头添加 \n
。
例如,如果您的错误是Error
,只需将其更改为\nError
。
如果您想更改 \n
的大小,您可以使用:
String newLine = "\n";
String errorMessage = "Error";
SpannableString spannableErrorMessage = new SpannableString(newLine + errorMessage );
spannableName.setSpan(new TextAppearanceSpan(activity, R.style.my_style),0,a.length(), 0);
spannableName.setSpan(new RelativeSizeSpan(2.0f), a.length(), a.length() + 1, 0);
如果您多次在 TextInputLayout 上设置错误,错误消息文本会被截断。 所以每次设置 TextInputLayout 错误消息文本时,只需先将其设置为 null 即可。
textInputLayout.setError(空);
textInputLayout.setError启用(假);<br>
textInputLayout.setError("Error Message");
我通过保存和恢复 LayoutParams 解决了问题:
final ViewGroup.LayoutParams params = textInputLayout.getLayoutParams();
textInputLayout.setError(errorMessage);
textInputLayout.setLayoutParams(params);
正如一些人已经指出的那样,这是一个已知错误。参见 https://issuetracker.google.com/issues/116747167
我通过这样做解决了这个问题(注意下面的代码是 Kotlin,所以 != 适用于字符串):
if(myInputLayout.error != error){
myInputLayout.error = error
}
您可以使用这个 Kotlin 扩展:
/**
* Fix a bug with [TextInputLayout] that cut the error text when setting the same text twice.
*/
fun TextInputLayout.setFixedError(errorTxt: CharSequence?) {
if (error != errorTxt) {
error = errorTxt
}
}
用法:
yourTextInput.setFixedError("Your error text")