Android – 如何使TextInputEditText的高度正好为2行?

huangapple go评论59阅读模式
英文:

Android - How to make the height of a TextInputEditText to be exactly of 2 lines?

问题

我有一个像这样的布局:

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/user_description_input_layout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="16dp"
        android:layout_marginTop="16dp"
        android:hint="描述"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:startIconContentDescription="Lalala">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/user_description_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="描述" />

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

它的高度正好是一行的大小,但我希望它可以正好是2行的大小。

我尝试在我的TextInputEditText标签中添加以下属性:

<com.google.android.material.textfield.TextInputEditText
    ...
    android:maxLines="2"
    android:scrollbars="vertical" />

但这使它以1行高度开始,当用户在上面键入时停在2行。我希望它从一开始就固定为2行,即使没有足够的文本需要2行。

我还希望它具有固定的大小,并允许用户在添加大于2行的文本时垂直滚动。

我知道我可以通过添加足够的字符使其具有2行高度,然后固定这个高度大小,然后清除TextInputEditText来以编程方式实现,但那是一个非常丑陋的解决方案。

英文:

I have a layout like this:

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/user_description_input_layout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="16dp"
        android:layout_marginTop="16dp"
        android:hint="Description"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:startIconContentDescription="Lalala">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/user_description_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="description" />

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

It's height is exactly the size of ONE line, but I wish it could be the size of 2 line exactly.

I tried adding theses attributes to my TextInputEditText tag:

<com.google.android.material.textfield.TextInputEditText
    ...
    android:maxLines="4"
    android:scrollbars="vertical" />

But that made it to start as a 1 line height and stop at 2 as the user types on it. I would like it to be fixed on 2 from the begining, even if it did not have enough text to need 2 lines.

I also would like it to have a fixed size and allow the user to scroll vertically in case he add some text that is larger than 2 lines.

I know I COULD do it programatically by adding enough caracters until it has a 2 lines height and fix this heigh size and then clean the TextInputEditText, but that is such an ugly solution.

答案1

得分: 3

尝试这样做,在你的 TextInputEditText 中:

android:minLines="2"
android:gravity="top"
英文:

Try this, in your TextInputEditText

android:minLines="2"
android:gravity="top"

huangapple
  • 本文由 发表于 2023年6月1日 19:57:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381619.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定