我一直在看Facebook评论活动:
我一直想知道他们如何设法使edittext的大小从一行增加到最多4行,同时,包含这些帖子的recyclerview也会向上调整并减小大小。当您输入文字时,这会自动发生。
我正在尝试通过执行以下操作自己复制其布局,但我不能显示多于2行。它一直将自身限制为“发送”箭头的大小,但在下面的示例中,edittext永远不会超过recyclerview。
以下是我的xml布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/white"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/like_box"
android:text="Number of Likes goes here"
android:maxLines="1"
android:minLines="1"
android:layout_margin="16dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="0.2dp"
android:id="@+id/divider"
android:layout_below="@+id/like_box"
android:background="@color/half_black"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_below="@+id/divider"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/commenttext" />
<View
android:layout_width="match_parent"
android:layout_height="0.2dp"
android:id="@+id/divider2"
android:layout_below="@+id/my_recycler_view"
android:background="@color/half_black"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/commenttext"
android:layout_toLeftOf="@+id/send"
android:background="@null"
android:paddingLeft="16dp"
android:textSize="16sp"
android:hint="Add a comment here"
android:layout_alignTop="@+id/send"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/send"
android:src="@drawable/ic_action_send_now"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Does anyone know what the correct xml layout code is to ensure that both the edittext and recyclerview adjust upwards when text is entered? I thought it would just be something simple like setting "wrap_content" to my edittext which I have already done but it still does not adjust properly.
Anoop M's answer seems to be the closest to what I want to do but the recyclerview does not adjust properly - see image below. Imagine if the blue portion of the screen was filled with text, the edittext will end up hiding the text in the blue section when you enter more than 4 lines. The edittext just seems to go over the recyclerview. This does not happen on the facebook app, comments are fully shown as the recyclerview adjust upwards when the edittext size increases.
我建议使用aLinearLayout
作为父容器。然后,可以通过layout_weights
在所有子视图上应用来获得所需的结果:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/like_box"
android:text="Number of Likes goes here"
android:maxLines="1"
android:minLines="1"
android:layout_margin="16dp"
android:layout_weight="0"/>
<View
android:layout_width="match_parent"
android:layout_height="0.2dp"
android:id="@+id/divider"
android:background="@color/half_black"
android:layout_weight="0"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<View
android:layout_width="match_parent"
android:layout_height="0.2dp"
android:id="@+id/divider2"
android:background="@color/half_black"
android:layout_weight="0"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/commenttext"
android:background="@null"
android:paddingLeft="16dp"
android:textSize="16sp"
android:maxLines="4"
android:inputType="textMultiLine"
android:hint="Add a comment here" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/send"
android:src="="@drawable/ic_action_send_now"
android:layout_weight="0" />
</LinearLayout>
</LinearLayout>
显着变化:
LinearLayout
layout_weights
分配EditText
和send
按钮已被容纳在水平内LinearLayout
。maxLines
的属性EditText
已4
根据您的要求设置。这样,EditText
遗嘱的高度会不断增长,直到其4行高,然后开始滚动。该send
按钮将保持与第一行对齐。inputType="textMultiLine"
已添加到中,EditText
以表明其内容EditText
可以跨越多行。RecyclerView
当EditText's
行数增加时,它将缩小,反之亦然。本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句