簡體   English   中英

如何縮小TextInputLayout錯誤文本的填充/邊距頂部和底部?

[英]How to shrink padding/ margin top and bottom of TextInputLayout error text?

它導致TextInputLayout垂直占用太多空間。
令人發瘋的是,TextInputLayout高度的一半僅用於錯誤文本(在EditText的水平線下方)。 錯誤文本的字體大小僅為11dp。 理想情況下,我希望將錯誤文本頂部和底部的空間縮小到像2dp這樣的極小

我已經嘗試了一切,但我只是設法將TextInputLayout的高度降低到70dp。

還有什么我可以做的嗎?

在此輸入圖像描述

布局:

<?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上的錯誤。 它在某種程度上有所幫助。

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
    }

您可以使用這兩個函數來設置錯誤,並在需要時使用文本觀察器清除它。 不理想,不回答你的問題,但對於路人,這可能有助於解決他們的用例。

textwatcher代碼,你需要它

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中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM