Android 布局:评论 activity 类似于 Facebook 应用
Android Layout: Comments activity similar to the Facebook app
我一直在看Facebook评论activity:
我一直想知道他们是如何设法让edittext的大小从一行增加到最多4行,同时包含帖子的recyclerview也向上调整并缩小大小。当您输入文本时,这会自动发生。
我正在尝试通过执行以下操作自己复制他们的布局,但我无法显示超过 2 行。它一直将自己限制在 "Send" 箭头的大小,但在我下面的示例中,编辑文本永远不会超过 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>
有谁知道正确的 xml 布局代码是什么,以确保在输入文本时 edittext 和 recyclerview 都向上调整?我认为这只是一些简单的事情,比如将 "wrap_content" 设置为我已经完成的编辑文本,但它仍然无法正确调整。
Anoop M 的回答似乎与我想要做的最接近,但是 recyclerview 没有正确调整 - 见下图。想象一下,如果屏幕的蓝色部分被文本填满,当您输入超过 4 行时,edittext 最终会将文本隐藏在蓝色部分。 edittext 似乎越过了 recyclerview。这不会发生在 facebook 应用程序上,当 edittext 大小增加时,评论会随着 recyclerview 向上调整而完整显示。
您需要为编辑文本设置 singleLine = false 属性
<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:singleLine="false"
android:hint="Add a comment here"
android:layout_alignTop="@+id/send"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
试试这个。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="65dp"
android:fadeScrollbars="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<LinearLayout
android:id="@+id/topedittext_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical">
<!--Your Contents Goes Here-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Your Contents Goes Here" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:id="@+id/tos_text_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@null"
android:gravity="center"
android:hint="Type message"
android:maxLines="4"
android:minHeight="60dp"
android:padding="7dp"
android:singleLine="false"
android:textColor="#88000000"
android:textColorHint="#bdbdbd"
android:textSize="15sp" />
</RelativeLayout>
</RelativeLayout>
要在 XML 中设置最大和最小线数,请使用。
android:maxlines="your choice" // 整数
android:minlines="your choice" // 整数
或者如果您想在一行中显示文本,请使用:
android:椭圆
当您在清单中定义 activity 时添加下面给出的代码,
<activity
android:name="com.companyname.applicationname"
android:windowSoftInputMode="adjustResize|adjustPan">
您还可以使用下面给出的编辑文本。
<EditText
android:inputType="textMultiLine" <!-- Multiline input -->
android:lines="8" <!-- Total Lines prior display -->
android:minLines="6" <!-- Minimum lines -->
android:gravity="top|left" <!-- Cursor Position -->
android:maxLines="10" <!-- Maximum Lines -->
android:layout_height="wrap_content" <!-- Height determined by content -->
/>
我建议使用 LinearLayout
作为父容器。然后,您可以通过在所有子视图上应用 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
的内容可以跨越多行。
- 当
EditText's
行数增加时,RecyclerView
会缩小,反之亦然。
我一直在看Facebook评论activity:
我一直想知道他们是如何设法让edittext的大小从一行增加到最多4行,同时包含帖子的recyclerview也向上调整并缩小大小。当您输入文本时,这会自动发生。
我正在尝试通过执行以下操作自己复制他们的布局,但我无法显示超过 2 行。它一直将自己限制在 "Send" 箭头的大小,但在我下面的示例中,编辑文本永远不会超过 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>
有谁知道正确的 xml 布局代码是什么,以确保在输入文本时 edittext 和 recyclerview 都向上调整?我认为这只是一些简单的事情,比如将 "wrap_content" 设置为我已经完成的编辑文本,但它仍然无法正确调整。
Anoop M 的回答似乎与我想要做的最接近,但是 recyclerview 没有正确调整 - 见下图。想象一下,如果屏幕的蓝色部分被文本填满,当您输入超过 4 行时,edittext 最终会将文本隐藏在蓝色部分。 edittext 似乎越过了 recyclerview。这不会发生在 facebook 应用程序上,当 edittext 大小增加时,评论会随着 recyclerview 向上调整而完整显示。
您需要为编辑文本设置 singleLine = false 属性
<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:singleLine="false"
android:hint="Add a comment here"
android:layout_alignTop="@+id/send"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
试试这个。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="65dp"
android:fadeScrollbars="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<LinearLayout
android:id="@+id/topedittext_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical">
<!--Your Contents Goes Here-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Your Contents Goes Here" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:id="@+id/tos_text_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@null"
android:gravity="center"
android:hint="Type message"
android:maxLines="4"
android:minHeight="60dp"
android:padding="7dp"
android:singleLine="false"
android:textColor="#88000000"
android:textColorHint="#bdbdbd"
android:textSize="15sp" />
</RelativeLayout>
</RelativeLayout>
要在 XML 中设置最大和最小线数,请使用。
android:maxlines="your choice" // 整数 android:minlines="your choice" // 整数
或者如果您想在一行中显示文本,请使用:
android:椭圆
当您在清单中定义 activity 时添加下面给出的代码,
<activity
android:name="com.companyname.applicationname"
android:windowSoftInputMode="adjustResize|adjustPan">
您还可以使用下面给出的编辑文本。
<EditText
android:inputType="textMultiLine" <!-- Multiline input -->
android:lines="8" <!-- Total Lines prior display -->
android:minLines="6" <!-- Minimum lines -->
android:gravity="top|left" <!-- Cursor Position -->
android:maxLines="10" <!-- Maximum Lines -->
android:layout_height="wrap_content" <!-- Height determined by content -->
/>
我建议使用 LinearLayout
作为父容器。然后,您可以通过在所有子视图上应用 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
的内容可以跨越多行。- 当
EditText's
行数增加时,RecyclerView
会缩小,反之亦然。