根据子 TextView 内容的大小缩放 RelativeLayout
Scaling RelativeLayout according to size of child TextView's content
我有一个消息 activity,我希望用户能够在其中输入 messageField
并让它随着文本流入下一行而动态增长。我还需要限制它应该增长多少,因为用户仍然应该看到发送的最后几条消息。我不知道从哪里开始。任何方向将不胜感激。
我的相关内容chat_activity.xml:(这都主持一个RelativeLayout
)
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/interactionHolder"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:layout_below="@+id/headerContainer"
android:id="@+id/contentFrameLayout">
<ListView
android:id="@+id/messageListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:transcriptMode="disabled"
android:smoothScrollbar="false"
android:dividerHeight="5dp"
android:divider="@null"
android:drawSelectorOnTop="false"
android:listSelector="@android:color/transparent"
android:cacheColorHint="#00000000" />
</FrameLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="@+id/interactionHolder"
android:layout_alignParentBottom="true"
android:visibility="visible">
<RelativeLayout
android:id="@+id/textfieldcontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="false"
android:layout_alignParentRight="false"
android:layout_gravity="bottom"
android:alignmentMode="alignMargins"
android:background="#ff9d9d9d"
>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/photoButton"
android:scaleType="centerCrop"
android:src="@android:drawable/ic_menu_camera"
android:layout_alignParentLeft="true"
android:background="#ff9d9d9d"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:contentDescription="@string/upload" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/voiceButton"
android:layout_toRightOf="@+id/photoButton"
android:focusable="false"
android:src="@android:drawable/ic_btn_speak_now"
android:scaleType="centerCrop"
android:background="#ff9d9d9d"
android:layout_centerVertical="true"
android:contentDescription="@string/voicenote" />
<EditText
android:id="@+id/messageField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:ems="10"
android:gravity="left|center"
android:inputType="textMultiLine"
android:background="@android:drawable/editbox_background_normal"
android:textColor="#ff000000"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/voiceButton"
android:layout_toEndOf="@+id/voiceButton"
android:layout_toLeftOf="@+id/sendButton"
android:layout_toStartOf="@+id/sendButton" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sendButton"
android:src="@android:drawable/ic_menu_send"
android:layout_alignParentRight="true"
android:background="#ff9d9d9d"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:contentDescription="@string/sendbutton"
android:clickable="true" />
</RelativeLayout>
</RelativeLayout>
我已经用它来聊天 activity。检查此控件。
<EditText
android:id="@+id/messageField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="@null"
android:hint="Message"
android:inputType="textMultiLine|textCapSentences"
android:maxHeight="100dp"
android:maxLength="8000"
android:minLines="1"
android:padding="10dp"
android:scrollbars="vertical"
android:textColor="@color/black"
android:textColorHint="@color/gray_60"
android:layout_toRightOf="@+id/voiceButton"
android:layout_toEndOf="@+id/voiceButton"
android:layout_toLeftOf="@+id/sendButton"
android:layout_toStartOf="@+id/sendButton"
android:textSize="15dp" />
您可以使用它的以下两个 xml 属性来限制 EditText
中的用户输入:
android:maxLines="[int]" // No. of lines
android:maxLength="[int]" // No. of characters
此小部件的缩放比例也将根据用户输入进行相应调整。
[编辑]
要使 RelativeLayout
与 EdditText
一起扩展(在一定程度上受到限制),您可以尝试如下更改布局参数:
<!-- This was orininally RelativeLayout, changing this to FrameLayout for performance reasons -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" <!-- change here -->
android:id="@+id/interactionHolder"
android:layout_alignParentBottom="true"
android:visibility="visible">
<RelativeLayout
android:id="@+id/textfieldcontainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" <!-- change here -->
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="false"
android:layout_alignParentRight="false"
android:layout_gravity="bottom"
android:alignmentMode="alignMargins"
android:background="#ff9d9d9d"
>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/photoButton"
android:scaleType="centerCrop"
android:src="@android:drawable/ic_menu_camera"
android:layout_alignParentLeft="true"
android:background="#ff9d9d9d"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:contentDescription="@string/upload" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/voiceButton"
android:layout_toRightOf="@+id/photoButton"
android:focusable="false"
android:src="@android:drawable/ic_btn_speak_now"
android:scaleType="centerCrop"
android:background="#ff9d9d9d"
android:layout_centerVertical="true"
android:contentDescription="@string/voicenote" />
<EditText
android:id="@+id/messageField"
android:layout_width="wrap_content"
android:layout_height="wrap_content" <!-- change here -->
android:maxLines="4" <!-- change here -->
android:ems="10"
android:gravity="left|center"
android:inputType="textMultiLine"
android:background="@android:drawable/editbox_background_normal"
android:textColor="#ff000000"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/voiceButton"
android:layout_toEndOf="@+id/voiceButton"
android:layout_toLeftOf="@+id/sendButton"
android:layout_toStartOf="@+id/sendButton" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sendButton"
android:src="@android:drawable/ic_menu_send"
android:layout_alignParentRight="true"
android:background="#ff9d9d9d"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:contentDescription="@string/sendbutton"
android:clickable="true" />
</RelativeLayout>
</FrameLayout>
我有一个消息 activity,我希望用户能够在其中输入 messageField
并让它随着文本流入下一行而动态增长。我还需要限制它应该增长多少,因为用户仍然应该看到发送的最后几条消息。我不知道从哪里开始。任何方向将不胜感激。
我的相关内容chat_activity.xml:(这都主持一个RelativeLayout
)
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/interactionHolder"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:layout_below="@+id/headerContainer"
android:id="@+id/contentFrameLayout">
<ListView
android:id="@+id/messageListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:transcriptMode="disabled"
android:smoothScrollbar="false"
android:dividerHeight="5dp"
android:divider="@null"
android:drawSelectorOnTop="false"
android:listSelector="@android:color/transparent"
android:cacheColorHint="#00000000" />
</FrameLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="@+id/interactionHolder"
android:layout_alignParentBottom="true"
android:visibility="visible">
<RelativeLayout
android:id="@+id/textfieldcontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="false"
android:layout_alignParentRight="false"
android:layout_gravity="bottom"
android:alignmentMode="alignMargins"
android:background="#ff9d9d9d"
>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/photoButton"
android:scaleType="centerCrop"
android:src="@android:drawable/ic_menu_camera"
android:layout_alignParentLeft="true"
android:background="#ff9d9d9d"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:contentDescription="@string/upload" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/voiceButton"
android:layout_toRightOf="@+id/photoButton"
android:focusable="false"
android:src="@android:drawable/ic_btn_speak_now"
android:scaleType="centerCrop"
android:background="#ff9d9d9d"
android:layout_centerVertical="true"
android:contentDescription="@string/voicenote" />
<EditText
android:id="@+id/messageField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:ems="10"
android:gravity="left|center"
android:inputType="textMultiLine"
android:background="@android:drawable/editbox_background_normal"
android:textColor="#ff000000"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/voiceButton"
android:layout_toEndOf="@+id/voiceButton"
android:layout_toLeftOf="@+id/sendButton"
android:layout_toStartOf="@+id/sendButton" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sendButton"
android:src="@android:drawable/ic_menu_send"
android:layout_alignParentRight="true"
android:background="#ff9d9d9d"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:contentDescription="@string/sendbutton"
android:clickable="true" />
</RelativeLayout>
</RelativeLayout>
我已经用它来聊天 activity。检查此控件。
<EditText
android:id="@+id/messageField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="@null"
android:hint="Message"
android:inputType="textMultiLine|textCapSentences"
android:maxHeight="100dp"
android:maxLength="8000"
android:minLines="1"
android:padding="10dp"
android:scrollbars="vertical"
android:textColor="@color/black"
android:textColorHint="@color/gray_60"
android:layout_toRightOf="@+id/voiceButton"
android:layout_toEndOf="@+id/voiceButton"
android:layout_toLeftOf="@+id/sendButton"
android:layout_toStartOf="@+id/sendButton"
android:textSize="15dp" />
您可以使用它的以下两个 xml 属性来限制 EditText
中的用户输入:
android:maxLines="[int]" // No. of lines
android:maxLength="[int]" // No. of characters
此小部件的缩放比例也将根据用户输入进行相应调整。
[编辑]
要使 RelativeLayout
与 EdditText
一起扩展(在一定程度上受到限制),您可以尝试如下更改布局参数:
<!-- This was orininally RelativeLayout, changing this to FrameLayout for performance reasons -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" <!-- change here -->
android:id="@+id/interactionHolder"
android:layout_alignParentBottom="true"
android:visibility="visible">
<RelativeLayout
android:id="@+id/textfieldcontainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" <!-- change here -->
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="false"
android:layout_alignParentRight="false"
android:layout_gravity="bottom"
android:alignmentMode="alignMargins"
android:background="#ff9d9d9d"
>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/photoButton"
android:scaleType="centerCrop"
android:src="@android:drawable/ic_menu_camera"
android:layout_alignParentLeft="true"
android:background="#ff9d9d9d"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:contentDescription="@string/upload" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/voiceButton"
android:layout_toRightOf="@+id/photoButton"
android:focusable="false"
android:src="@android:drawable/ic_btn_speak_now"
android:scaleType="centerCrop"
android:background="#ff9d9d9d"
android:layout_centerVertical="true"
android:contentDescription="@string/voicenote" />
<EditText
android:id="@+id/messageField"
android:layout_width="wrap_content"
android:layout_height="wrap_content" <!-- change here -->
android:maxLines="4" <!-- change here -->
android:ems="10"
android:gravity="left|center"
android:inputType="textMultiLine"
android:background="@android:drawable/editbox_background_normal"
android:textColor="#ff000000"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/voiceButton"
android:layout_toEndOf="@+id/voiceButton"
android:layout_toLeftOf="@+id/sendButton"
android:layout_toStartOf="@+id/sendButton" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sendButton"
android:src="@android:drawable/ic_menu_send"
android:layout_alignParentRight="true"
android:background="#ff9d9d9d"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:contentDescription="@string/sendbutton"
android:clickable="true" />
</RelativeLayout>
</FrameLayout>