为聊天创建可滚动布局

时间:2013-03-22 23:26:08

标签: android chat

我一直在寻找一种在我的应用程序中实现聊天的方法。我已经实现了一种获取传入消息和传出消息的方法。所以我唯一的问题是如何为聊天创建布局。但我真的无法弄明白。 看完之后,我发现了一个看起来像聊天的布局(http://code.google.com/p/simple-android-instant-messaging-application/source/browse/trunk/res/layout/messaging_screen.xml?r=6):

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical"
        android:padding="10dip" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <!--    
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:text="Friend:"
    />

    <EditText android:id="@+id/friendUserName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:singleLine="true"
        android:editable="false" />
    --> 
    <TextView   android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="10dip"
                        android:text="Messages:"/>

    <EditText   android:id="@+id/messageHistory"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:clickable="true"        
                        android:layout_weight="1"
                    android:editable="false"
                    android:gravity="top"
                    android:scrollbars="vertical"
                    android:scrollbarSize="10px"
                    /> 

    <LinearLayout android:orientation="horizontal"
                          android:layout_width="fill_parent"
                          android:layout_height="fill_parent"
                          android:layout_weight="4">

                        <EditText       android:id="@+id/message"
                                                android:layout_width="fill_parent"
                                        android:layout_height="fill_parent"
                                        android:gravity="top"
                                        android:layout_weight="1"
                                        />

                    <Button android:id="@+id/sendMessageButton"
                                android:layout_width="fill_parent"
                                android:layout_height="fill_parent"
                                android:layout_weight="4"
                                android:text="Send"/>

        </LinearLayout>

</LinearLayout>

我不知道我是否以错误的方式使用它,但每个传入的消息总是出现在同一文本视图中,覆盖之前的消息,使用新的消息。我想我可能需要为JAVA方面做更多的事情。

换句话说,我想要的是消息看似可滚动的地方,并且每个到达的新消息应该出现在右侧,并且一个接一个地出现。传出消息应显示在左侧。我当然是在谈论i-phone等短信应用程序的外观。

我不明白该怎么做。如果有人知道解决方法或任何有关如何实现这一点的信息,那将非常受欢迎。

2 个答案:

答案 0 :(得分:1)

您需要使用ListView实现一个屏幕,以显示所有单独的聊天消息历史记录,并在底部显示EditText,以便用户输入新文本。可能有各种支持UI元素,例如顶部的标题和发送&#39;按钮。

如果需要,每个ListView项目都会显示消息以及日期戳和其他信息。您可以为传入和传出消息使用不同的背景。

找到ListView的任何示例并从那里开始。

答案 1 :(得分:0)

您想要的是显示您的消息的ListView。任何消息都将是ListView中的项目。对于内容,您必须实现一个知道您的消息数据结构的Adapter。 ListView,适配器和ListView中的自定义视图的好例子是http://www.ezzylearning.com/tutorial.aspx?tid=1763429