【问题标题】:TextInputLayout error text paddingTextInputLayout 错误文本填充
【发布时间】:2023-03-17 02:30:02
【问题描述】:

我将 TextInputLayoutAppCompatEditText 一起使用,该 AppCompatEditText 具有基于自定义 xml 形状的背景。每当我设置一些错误时,错误行从布局的开头开始。有什么方法可以填充该错误行。

【问题讨论】:

  • 你能用你试过的代码更新这个问题吗?
  • 使用setError(null)
  • 删除背景图片到 EditText 它解决了你的问题背景图片到 EditText 创建这个问题

标签: android material-design android-appcompat android-textinputlayout


【解决方案1】:
<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
【解决方案2】:

您可以使用以下方法引用错误 TextView:

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

然后你就可以使用它的布局参数了。

【讨论】:

  • 冒险黑客是有风险的。同时,我认为 id 不会很快改变
  • 请记住,错误文本视图仅在显示错误时创建,因此您需要在 TextInputLayout 中显示错误后更改参数。
【解决方案3】:

我努力解决这个问题,这对我有用:

删除我的错误文本视图的父级上的填充

使用this solution 我可以找到错误文本视图(因为我需要向它添加一个图标)但无法摆脱填充,所以经过几次尝试后,我意识到 TextInputLayout 有 2 个子元素,因此删除了填充从第二个孩子开始就成功了 为了确定,我也从第一个中删除了它

使用 Kotlin 扩展我想出了一个干净的解决方案

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

你也可以在java中使用相同的逻辑来做到这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-12
    • 2020-11-03
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 2017-05-15
    • 1970-01-01
    相关资源
    最近更新 更多