TextInputLayout 计数器和边距问题

TextInputLayout counter and margin problem

当使用 TextInputLayout 并将高度设置为 wrap_content 的任何其他值时,计数器会消失,我还想将其与 ConstraintLayout 一起使用,并将其高度设置为 0dp ,随着计数器消失,顶部有这个边距。可以将 TextInputLayout 的高度设置为 0dp 且计数器可见且顶部没有此边距吗?

<android.support.constraint.ConstraintLayout
    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="match_parent"
    >

    <android.support.design.widget.TextInputLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="50dp"
        app:counterEnabled="true"
        app:counterMaxLength="180"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#2342"
            android:gravity="top"
            android:inputType="textMultiLine|textNoSuggestions" />


    </android.support.design.widget.TextInputLayout>

</android.support.constraint.ConstraintLayout>

我明白了,您可能需要重新考虑布局设计。问题不是 TextInputLayout,而是 TextInputEditText。当您将 TextInputEditText 设置为匹配父级时,它匹配高度 TextInputLayout,因此 TextInputLayout 中没有剩余 space 来绘制计数器。因此,如果您将 TextInputEditText 的高度值更改为任何其他值,您将看到计数器。

我的建议是,您要么需要重新考虑设计的布局,要么必须以编程方式设置 InputEditText 的高度,具体取决于 activity 的 window 可见高度。

我发现 TextInputLayout 继承自 LinearLayout。所以我尝试了一些 layout_heightlayout_weight 的组合。最后,我找到了一个解决方案,使 TextInputLayout 可以具有任何类型的高度,并且 counter 仍然可见。

TextInputEditText中,你必须设置android:layout_height="match_parent"android:layout_weight="1"

是的,这很奇怪,但它适用于 API 30 和 API 27

截图

示例代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
    android:padding="12dp"
    android:orientation="vertical">

    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:hint="Label"
        app:counterEnabled="true"
        app:counterMaxLength="10000">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="start"
            android:maxLength="10000" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.checkbox.MaterialCheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Checkbox" />

</LinearLayout>