首先看一下最终的效果,显示了消息时间,用户昵称,用户头像。
大致实现方法:
用最简单的ListView显示消息内容。
不同的用户使用不同的消息布局文件,从而达到头像左右显示的效果,如上图有2个用户"Tony","Hill",头像分别显示在左右两边。
代码文件清单:
主布局文件activity_main.xml:
1 <RelativeLayout xmlns:andro 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="#ffffff" 6 tools:context=".MainActivity" > 7 <ListView 8 android: 9 android:layout_height="fill_parent" 10 android:layout_width="fill_parent" 11 android:layout_above="@+id/ll_send_1" 12 android:scrollbars="none" 13 android:divider="@null" 14 android:footerDividersEnabled="false"> 15 </ListView> 16 <LinearLayout 17 android: 18 android:layout_height="50dp" 19 android:layout_width="fill_parent" 20 android:orientation="horizontal" 21 android:layout_above="@+id/ll_send_2"> 22 <EditText 23 android: 24 android:layout_width="0dp" 25 android:layout_height="fill_parent" 26 android:layout_weight="3" 27 android:hint="Tony请输入"/> 28 <Button 29 android: 30 android:layout_height="fill_parent" 31 android:layout_width="0dp" 32 android:layout_weight="1" 33 android:text="send"/> 34 </LinearLayout> 35 <LinearLayout 36 android: 37 android:layout_height="50dp" 38 android:layout_width="fill_parent" 39 android:orientation="horizontal" 40 android:layout_alignParentBottom="true"> 41 <EditText 42 android: 43 android:layout_width="0dp" 44 android:layout_height="fill_parent" 45 android:layout_weight="3" 46 android:hint="Hill请输入"/> 47 <Button 48 android: 49 android:layout_height="fill_parent" 50 android:layout_width="0dp" 51 android:layout_weight="1" 52 android:text="send"/> 53 </LinearLayout> 54 </RelativeLayout>