提问者:小点点

我一直在尝试将文本换行添加到我的edittext中,但看起来没有什么是正确的


好吧,让我给你一些上下文。我正在制作一个消息应用程序(你可以从我的其他帖子中看出),现在我正在开发一个编辑消息的函数。显示的对话框有一个enter按钮,但我的消息框没有,它有一个“Done”按钮。我将该文本框的imeOptions设置为有一个enter按钮,并配置了一些其他属性以使其工作。

但出于某种原因,没有什么东西看起来光鲜亮丽。我试过查看其他StackOverflow帖子,查看谷歌,改变我的drawables(我用它作为编辑文本的背景),但看起来并不像我预期的那样。我希望edittext在消息框达到4行左右时保持固定的高度,并且附件图标始终保持在相同的位置,而不是扩大高度。

下面是我的ChatActivity XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".chats.ChatActivity"
    android:background="?attr/bgColor"
>

    <LinearLayout
        android:id="@+id/llProgress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <LinearLayout
        android:id="@+id/llSendChat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="12dp"
            android:layout_weight="1"
            android:background="@drawable/gray_circle"
            android:orientation="horizontal"
            android:padding="3dp">

            <EditText
                android:id="@+id/etMessage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:background="@drawable/gray_circle"
                android:ems="10"
                android:hint="@string/enter_message"
                android:imeOptions="actionNone"
                android:inputType="textMultiLine"
                android:padding="8dp"
                android:textColor="?attr/messageEditTextColor"
                android:textColorHint="?attr/hintTextColor"
                android:textSize="14sp" />

            <ImageView
                android:id="@+id/ivAttachment"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="?attr/selectableItemBackground"
                android:paddingLeft="8dp"
                android:paddingRight="8dp"
                android:tint="?attr/messageEditAttachmentTint"
                app:srcCompat="@drawable/ic_attachment" />
        </LinearLayout>

        <ImageView
            android:id="@+id/ivSend"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/red_circle"
            android:foreground="?android:attr/selectableItemBackground"
            android:padding="8dp"
            android:tint="?attr/sendButtonIconColor"
            app:srcCompat="@drawable/ic_send"
            android:layout_gravity="right|bottom"/>
    </LinearLayout>

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/srlMessages"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/llSendChat"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/llProgress">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvMessages"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
        />
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

共1个答案

匿名用户

在EditText中设置最大行数:

android:maxLines="5" 

或者任何数字而不是5。