简体   繁体   English

EditText视图不在新行上扩展

[英]EditText view not expanding on new line

I'm trying to get user comment and after pressing the enter button, the text just gets hide due to a line change. 我正在尝试获取用户评论,然后按Enter键后,由于换行而导致文本隐藏。 I m trying to expand my EditText View so that I can show at least 4 lines of comment. 我正在尝试扩展EditText视图,以便可以显示至少4行注释。

<LinearLayout
    android:id="@+id/comments_body_box"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:background="#fff"
    android:elevation="8dp"
    android:gravity="right"
    android:maxHeight="150dp"
    android:orientation="horizontal"
    android:weightSum="10"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <EditText
        android:id="@+id/comments_text_body"
        android:layout_width="151dp"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_weight="8"
        android:inputType="textCapSentences|textMultiLine"
        android:maxHeight="100dp"
        android:maxLength="2000"
        android:maxLines="4"
        android:paddingBottom="12dp"
        android:paddingEnd="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="16dp" />

    <ImageView
        android:id="@+id/comment_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="12dp"
        app:srcCompat="@drawable/ic_send_black_24dp" />

</LinearLayout>

Above is my current code, can someone tell me what's wrong with my XML file? 上面是我当前的代码,有人可以告诉我XML文件有什么问题吗?

I added the following two attributes too 我也添加了以下两个属性

android:maxLength="2000"
android:maxLines="4"

You need to add this into your EditText attribute 您需要将此添加到您的EditText属性中

  android:lines="4"

Also, set height to wrap_content 此外,将高度设置为wrap_content

android:layout_height="wrap_content"

First I cleaned up your code 首先,我清理了您的代码

<LinearLayout
android:id="@+id/comments_body_box"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="#fff"
android:elevation="8dp"
android:gravity="right"
android:maxHeight="150dp"
android:orientation="horizontal"
android:weightSum="10"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">

<EditText
    android:id="@+id/comments_text_body"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="8"
    android:inputType="textCapSentences|textMultiLine"
    android:maxHeight="100dp"
    android:maxLength="2000"
    android:maxLines="4"
    android:paddingBottom="12dp"
    android:paddingEnd="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="16dp" />

<ImageView
    android:id="@+id/comment_send"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:padding="12dp"
    app:srcCompat="@drawable/ic_send_black_24dp" />

1) You should use layout_width="0dp" as you use layout_weight in the LinearLayout 1)您应该在LinearLayout中使用layout_weight时使用layout_width =“ 0dp”

2) do NOT use android:layout_alignParentBottom="true" and android:layout_alignParentEnd="true" as it doesn't make sense in the LinearLayout 2) 不要使用android:layout_alignParentBottom =“ true”和android:layout_alignParentEnd =“ true”,因为它在LinearLayout中没有意义

3) And use layout_height="wrap_content" is the EditText 3)并使用layout_height =“ wrap_content”是EditText

It looks like you have the EditText set to match_parent . 看来您已将EditText设置为match_parent

Try changing android:layout_height="match_parent" to android:layout_height="wrap_content" 尝试将android:layout_height="match_parent"更改为android:layout_height="wrap_content"

Before starting, you need to make a few changes to your code. 开始之前,您需要对代码进行一些更改。

  1. If you are using android:layout_weight attribute, you should always set your width attribute as android:layout_width="0dp" 如果您使用的是android:layout_weight属性,则应始终将width属性设置为android:layout_width="0dp"
  2. Since you are using a LinearLayout remove the attributes android:layout_alignParentBottom and android:layout_alignParentEnd . 由于您使用的是LinearLayout,因此请删除android:layout_alignParentBottomandroid:layout_alignParentEnd They are invalid for this case. 在这种情况下,它们是无效的。
  3. Since you are using an ImageView under the EditText , it would be better if you assigned a height to your view instead of match_parent , or just use wrap_content if you don't need a fixed size. 由于您使用的是EditText下的ImageView ,因此最好为视图分配高度而不是match_parent ,或者如果不需要固定大小, match_parent使用wrap_content

You mentioned you needed to display at least 4 lines of text in your EditText . 您提到您需要在EditText中至少显示4行文本。 If that is required for all use cases, add the attribute android:minLines="4" to your EditText . 如果所有用例都需android:minLines="4" ,请将属性android:minLines="4"到您的EditText If you want a maximum of 4 lines to be displayed, then your code android:maxLines="4" works perfectly. 如果您希望最多显示4行,那么您的代码android:maxLines="4"可以完美地工作。

To prevent the issue where the text gets hidden as the number of lines increases, add the attribute android:scrollbars="vertical" to the EditText . 为了防止随着行数增加而隐藏文本的问题,请将属性android:scrollbars="vertical"EditText

Your final EditText XML may look something like (assuming 4 lines are needed at a minimum. If not, just remove the android:minLines attribute) 最终的EditText XML可能看起来像(假设至少需要4行。如果不需要,只需删除android:minLines属性)

        <EditText
            android:id="@+id/comments_text_body"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="8"
            android:inputType="textCapSentences|textMultiLine"
            android:maxHeight="100dp"
            android:minLength="2000"
            android:minLines="4"
            android:scrollbars="vertical"
            android:paddingBottom="12dp"
            android:paddingEnd="10dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="16dp" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM