我正在尝试获取用户评论,然后按Enter键后,由于换行而导致文本隐藏。一世 我试图扩展我的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>
以上是我当前的代码,有人可以告诉我XML文件有什么问题吗?
我也添加了以下两个属性
android:maxLength="2000"
android:maxLines="4"
答案 0 :(得分:1)
您需要将其添加到EditText属性中
android:lines="4"
还要将高度设置为 wrap_content
android:layout_height="wrap_content"
答案 1 :(得分:1)
首先我清理了您的代码
<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)您应该在LinearLayout中使用 layout_weight 时使用 layout_width =“ 0dp”
2)不要不要使用android:layout_alignParentBottom =“ true”和android:layout_alignParentEnd =“ true”,因为在LinearLayout中没有意义
3)并使用layout_height =“ wrap_content”是EditText
答案 2 :(得分:0)
您似乎已将EditText
设置为match_parent
。
尝试更改
android:layout_height="match_parent"
至
android:layout_height="wrap_content"
答案 3 :(得分:0)
开始之前,您需要对代码进行一些更改。
android:layout_weight
属性,则应始终将width属性设置为android:layout_width="0dp"
android:layout_alignParentBottom
和android:layout_alignParentEnd
。在这种情况下,它们无效。ImageView
下使用EditText
,因此,最好为视图分配高度而不是match_parent
,或者在情况下仅使用wrap_content
您不需要固定大小。您提到您需要在EditText
中显示至少4行文本。如果所有用例都需要这样做,请将属性android:minLines="4"
添加到您的EditText
中。如果您最多希望显示4行,那么您的代码android:maxLines="4"
可以完美地工作。
为防止出现文本随着行数增加而被隐藏的问题,请将属性android:scrollbars="vertical"
添加到EditText
。
您最终的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" />