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. I m trying to expand my EditText View so that I can show a...

问题:

I'm trying to get user comment and after pressing the enter button, the text just gets hide due to a line change. I m trying to expand my EditText View so that I can show at least 4 lines of comment.

<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?

I added the following two attributes too

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

回答1:

You need to add this into your EditText attribute

  android:lines="4"

Also, set height to wrap_content

android:layout_height="wrap_content"

回答2:

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

2) do NOT use android:layout_alignParentBottom="true" and android:layout_alignParentEnd="true" as it doesn't make sense in the LinearLayout

3) And use layout_height="wrap_content" is the EditText


回答3:

It looks like you have the EditText set to match_parent.

Try changing android:layout_height="match_parent" to android:layout_height="wrap_content"


回答4:

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"
  2. Since you are using a LinearLayout remove the attributes android:layout_alignParentBottom and android: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.

You mentioned you needed to display at least 4 lines of text in your EditText. If that is required for all use cases, add the attribute android:minLines="4" to your EditText. If you want a maximum of 4 lines to be displayed, then your code android:maxLines="4" works perfectly.

To prevent the issue where the text gets hidden as the number of lines increases, add the attribute android:scrollbars="vertical" to the 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
            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" />
  • 发表于 2018-07-07 01:33
  • 阅读 ( 273 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除