android开发 笔记(2.09)
操作流程:
1.准备好收发消息的背景图片
(上一篇有提到相关制作步骤)
2.在build.grade中添加依赖库
因为用到RecycleView
记得
3.编写主页面 activity_main.xml
在主页面中放置一个RecycleView用于显示聊天内容,放置一个EditText用于输入消息,Button用于send,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background">
<android.support.v7.widget.RecyclerView
android:id="@+id/msg_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_text"
android:layout_width="0dp"
android:layout_weight="1"
android:hint="Type something here"
android:layout_height="wrap_content"
android:maxLines="2"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:textAllCaps="false"/>
</LinearLayout>
</LinearLayout>
4.新建Msg
这个类中有两个字段,content表示消息的内容,type表示消息的类型(接收/发送)
相关代码如下:
public class Msg {
public static final int TYPE_RECEIVED = 0;
public static final int TYPE_SENT = 1;
private String conent;
private int type;
public Msg(String content, int type) {
this.conent = content;
this