【问题标题】:Create scrollable layout for chat为聊天创建可滚动布局
【发布时间】:2013-03-22 23:26:07
【问题描述】:

我正在寻找一种在我的应用程序中实现聊天的方法。我已经实现了一种获取传入消息和传出消息的方法。所以我唯一的问题是如何为聊天创建布局。但我真的想不通。 看了之后,我发现了一个看起来像聊天的布局(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 方面做更多的事情。

换句话说,我想要的是消息看起来可以滚动的地方,并且每条到达的新消息都应该出现在右侧,并且一个接一个。传出消息应出现在左侧。我说的当然是 iPhone 等 SMS 应用程序的外观。

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

【问题讨论】:

    标签: android chat


    【解决方案1】:

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

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

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

    【讨论】:

    • 我需要一个适配器和一个列表视图来显示传入和传出的消息吗?
    • 您将使用一个 ListView 来显示按时间排序的单个堆栈中的所有消息。您将需要做一些装饰以指示哪些列表项是传入的,哪些项是传出的。在您的第一个实现中,只需通过您将开始使用的 SimpleAdapter 将单词“传入”或“传出”放入列表项。之后,您可以根据传入/传出进行各种装饰,例如左/右对齐,不同的颜色,思想气泡。作为一个特别高级的练习,您可以使用 9-patch 背景,让您的聊天界面看起来像 iPhone。
    • 谢谢很多,我会尽量照你说的做
    • 你能看看这个stackoverflow.com/questions/15624324/…吗??
    • 您要求我查看的链接来自一个用户,该用户实现了 ListView 并出现 NullPointerException,因为他的 ArrayAdapter 使用了错误类型的上下文指针。他朝着正确的方向前进,可能很容易解决了他的问题。另一方面,您必须实现 ListView 或 GridView 才能获得所需的动态项目数列表。搜索“示例”或“教程”并在 StackOverflow.com 之外找到一些东西,这对于解决问题而不是寻找剪切和粘贴解决方案更好。您选择的 ListView 教程不一定适用于 Chat 视图。
    【解决方案2】:

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

    【讨论】:

    • 所以你的意思是不会使用 TextView 来显示消息??而且您还意味着我将为传入和传出消息创建一个 ListView,并且我必须在适配器中实现传入和传出消息的显示方式??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 2016-05-28
    相关资源
    最近更新 更多