簡體   English   中英

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中沒有剩余空間來繪制計數器。 因此,如果將TextInputEditText的高度值更改為任何其他值,您將看到計數器。

我的建議是,您要么需要重新考慮設計的布局,要么必須根據活動窗口的可見高度,以編程方式設置InputEditText的高度。

我發現TextInputLayout繼承自LinearLayout 所以我嘗試了一些layout_heightlayout_weight的組合。 最后,我找到了一個解決方案,使TextInputLayout可以具有任何類型的高度和計數器仍然可見。

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>

暫無
暫無

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

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