2016-08-02 148 views
0

我有一個消息傳遞活動,我希望用戶能夠輸入到messageField,並在文本流入下一行時使其動態增長。我還需要限制它應該增長多少,因爲用戶仍然應該看到發送的最後幾條消息。我不知道從哪裏開始。任何方向將不勝感激。我chat_activity.xml的根據子視圖的大小縮放RelativeLayout TextView的內容

相關內容:(在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> 

回答

1

可以使用下面的這兩個XML屬性限制用戶輸入您的EditText

android:maxLines="[int]" // No. of lines 
android:maxLength="[int]" // No. of characters 

這個小部件的比例也將相應用戶輸入來調整。

[編輯]

爲了使RelativeLayout隨着EdditText(限制在一定程度上)擴展,你可以嘗試改變你的佈局PARAMS如下:

<!-- 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> 
+0

謝謝,但它不是在我上面的設置中工作。我想我必須擴大我的相對佈局,但不知道如何? – Stephan

+1

@Stephan檢查我的帖子更新是否超過 – Shaishav

+0

謝謝,現在如何讓圖像按鈕保持在父母的底部? – Stephan

0

我已經使用這個對我的聊天活動。檢查這個控件。

<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" />