如何在TextInputLayout上设置最大高度?

3

我不想限制行数,而是希望限制TextInputLayout的最大高度,以便如果它太长,就会变成垂直滚动。

我可以接受在代码中解决这个问题。

目前maxHeight属性对TextInputLayoutEditText都没有影响。

这是我的布局:

<com.google.android.material.textfield.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:hint="@string/address"
    android:paddingTop="@dimen/margin_8"
    android:paddingBottom="@dimen/margin_8">

    <com.mamamia.mamamiarestaurant.ui.MultiLineTextInputEditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:imeOptions="actionNext"
        android:inputType="text|textPostalAddress|textMultiLine"
        android:maxLength="@integer/address_max_length" />
</com.google.android.material.textfield.TextInputLayout>

我将其包含在一个约束布局中

<include
    android:id="@+id/text_input_layout_address"
    layout="@layout/view_text_input_layout_address"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constrainedHeight="true"
    app:layout_constraintHeight_max="200dp" />

layout_constraintHeight_max没有效果。但是,如果将layout_constrainedHeight设置为true,则会使布局的高度固定(不是由layout_constraintHeight_max指定的高度,而是默认的固定编辑文本高度)。


1
谢谢,你的问题对我非常有帮助! - acmpo6ou
1个回答

0

layout_constraintHeight_max如果视图没有顶部或底部约束,则无法起作用,因此您可以将android:maxHeight放在InputEditText上,或者调整TextInputLayout上的约束:

<include
        layout="@layout/view_text_input_layout_address"
        android:id="@+id/text_input_layout_address"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHeight_default="wrap"
        app:layout_constraintHeight_max="200dp" />

如果父布局的高度是match_parent,那么你可能只需要使用app:layout_constraintHeight_default="wrap"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接