CardView 没有包装多行 EditText

CardView is not wrapping multiline EditText

我正在尝试将文本换行到 EditText 中,但它不起作用。我已经尝试了很多来自 Whosebug 的解决方案,但对我来说没有任何效果。

这是卡片视图布局中的 EditText 代码。

<androidx.cardview.widget.CardView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/spacing_small"
        android:layout_weight="1"
        app:cardBackgroundColor="@android:color/white"
        app:cardCornerRadius="18dp"
        app:cardElevation="1dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/spacing_xmlarge"
            android:orientation="horizontal"
            android:paddingLeft="@dimen/spacing_small"
            android:paddingRight="@dimen/spacing_small">

            <ImageView
                android:layout_width="@dimen/spacing_xmlarge"
                android:layout_height="@dimen/spacing_xmlarge"
                android:background="?attr/selectableItemBackground"
                android:clickable="true"
                android:padding="@dimen/spacing_middle"
                android:tint="#00897B"
                app:srcCompat="@drawable/ic_sentiment_satisfied" />

            <EditText
                android:id="@+id/edittext_chat_log"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"

                android:background="@android:color/transparent"
                android:gravity="top|left"
                android:hint="Write a message..."
                android:imeOptions="actionSearch"

                android:inputType="textMultiLine|textCapSentences"
                android:lines="5"
                android:maxLines="10"
                android:minLines="1"
                android:padding="@dimen/spacing_middle"
                android:scrollbars="none" />

            <ImageView
                android:id="@+id/photoPickerButton"
                android:layout_width="@dimen/spacing_xmlarge"
                android:layout_height="@dimen/spacing_xmlarge"
                android:background="?attr/selectableItemBackground"
                android:clickable="true"
                android:padding="@dimen/spacing_middle"
                android:tint="#00897B"
                app:srcCompat="@drawable/ic_photo_camera" />

        </LinearLayout>

    </androidx.cardview.widget.CardView>

这是完整的 xml 文件代码:

<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:background="#E8E9E4"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#075E54"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/lyt_back"
        android:layout_width="wrap_content"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/selectableItemBackground"
        android:clickable="true"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingLeft="@dimen/spacing_medium"
        android:paddingRight="@dimen/spacing_medium">

        <ImageButton
            android:id="@+id/chat_back_press"
            android:layout_width="wrap_content"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/transparent"
            app:srcCompat="@drawable/ic_arrow_back" />

        <View
            android:layout_width="@dimen/spacing_medium"
            android:layout_height="0dp" />

        <com.mikhaellopez.circularimageview.CircularImageView
            android:id="@+id/image"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:src="@drawable/photo_male_8"
            app:civ_border="true"
            app:civ_border_color="#075E54"
            app:civ_border_width="0dp"
            app:civ_shadow="true"
            app:civ_shadow_radius="0" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="@dimen/spacing_small"
        android:paddingRight="@dimen/spacing_small">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Roberts"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            android:textColor="@android:color/white"
            app:fontFamily="sans-serif-medium" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Online"
            android:textAppearance="@style/TextAppearance.AppCompat.Caption"
            android:textColor="@color/grey_10" />

    </LinearLayout>

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/chat_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:contentInsetStartWithNavigation="0dp"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

</LinearLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <!--        <android.support.v7.widget.RecyclerView-->
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview_chat_log"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingTop="@dimen/spacing_medium"
        android:paddingBottom="@dimen/spacing_medium"
        android:scrollbars="vertical"
        android:scrollingCache="true"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_alignParentTop="true"
        android:background="@drawable/bg_gradient_soft"
        android:rotation="180" />

</RelativeLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="@dimen/spacing_medium">

    <!--        <android.support.v7.widget.CardView-->
    <androidx.cardview.widget.CardView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/spacing_small"
        android:layout_weight="1"
        app:cardBackgroundColor="@android:color/white"
        app:cardCornerRadius="18dp"
        app:cardElevation="1dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/spacing_xmlarge"
            android:orientation="horizontal"
            android:paddingLeft="@dimen/spacing_small"
            android:paddingRight="@dimen/spacing_small">

            <ImageView
                android:layout_width="@dimen/spacing_xmlarge"
                android:layout_height="@dimen/spacing_xmlarge"
                android:background="?attr/selectableItemBackground"
                android:clickable="true"
                android:padding="@dimen/spacing_middle"
                android:tint="#00897B"
                app:srcCompat="@drawable/ic_sentiment_satisfied" />

            <EditText
                android:id="@+id/edittext_chat_log"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"

                android:background="@android:color/transparent"
                android:gravity="top|left"
                android:hint="Write a message..."
                android:imeOptions="actionSearch"

                android:inputType="textMultiLine|textCapSentences"
                android:lines="5"
                android:maxLines="10"
                android:minLines="1"
                android:padding="@dimen/spacing_middle"
                android:scrollbars="none" />

            <ImageView
                android:id="@+id/photoPickerButton"
                android:layout_width="@dimen/spacing_xmlarge"
                android:layout_height="@dimen/spacing_xmlarge"
                android:background="?attr/selectableItemBackground"
                android:clickable="true"
                android:padding="@dimen/spacing_middle"
                android:tint="#00897B"
                app:srcCompat="@drawable/ic_photo_camera" />

        </LinearLayout>

    </androidx.cardview.widget.CardView>

    <!--        <android.support.design.widget.FloatingActionButton-->
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/send_button_chat_log"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/spacing_small"
        android:clickable="true"
        android:gravity="center"
        android:tint="@android:color/white"
        app:backgroundTint="#00897B"
        app:elevation="1dp"
        app:fabSize="mini"
        app:srcCompat="@drawable/ic_mic"
        app:useCompatPadding="false" />

</LinearLayout>

在您的 EditText 中,尝试设置 singleLine=false。由于为 inputType 指定了一个值。 参考:https://developer.android.com/reference/android/widget/TextView.html#attr_android:singleLine android:单行 将文本限制为单个水平滚动行而不是让它换行到多行,并在您按回车键时提升焦点而不是插入换行符。对于不可编辑的文本,默认值为 false(多行换行文本模式),但如果您为 inputType 指定任何值,则默认值为 true(单行输入字段模式)。 可以是布尔值,例如"true"或"false".

<EditText
                android:id="@+id/edittext_chat_log"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"

                android:background="@android:color/transparent"
                android:gravity="top|left"
                android:hint="Write a message..."
                android:imeOptions="actionSearch"

                android:inputType="textMultiLine|textCapSentences"
                android:lines="5"
                android:maxLines="10"
                android:minLines="1"
                android:padding="@dimen/spacing_middle"
                android:scrollbars="none" />```




您的布局设计有几个问题。关键是 parent 限制了视图。 EditText 的高度设置为 match_parent。它将匹配您的线性布局。快速解决问题的方法就是修改以下语句。

  1. 内容布局也应该换行。

        <androidx.cardview.widget.CardView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/spacing_small"
            android:layout_weight="1"
            app:cardBackgroundColor="@android:color/white"
            app:cardCornerRadius="18dp"
            app:cardElevation="1dp">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" <!--here-->
    
  2. 你的 EditText 也应该换行。

                <EditText
                android:id="@+id/edittext_chat_log"
                android:layout_width="0dp"
                android:layout_height="wrap_content" <!--here-->
                android:layout_weight="1"
    
  3. 如果不想限制高度,请去掉android:lines="5"

  4. 如果要限制行数为10行,只需要保持android:lines="10"即可,不需要保持android:maxLines="10"和android:minLines="1"

那么问题就解决了。