Android用recyclerview编写聊天界面

本文介绍了如何在Android中使用RecyclerView来创建聊天界面。首先,通过Project Structure导入RecyclerView依赖。接着,提到了Nine-Patch图片工具,用于创建可拉伸的背景。然后详细讲述了如何编写XML布局文件、创建消息实体类Msg、适配器MsgAdapter,以及在MainActivity中进行相关代码实现。最终,运行程序将展示初始化的聊天记录,并支持用户输入和发送消息。
摘要由CSDN通过智能技术生成

编写聊天界面

开始工作,首先我们需要用到recyclerview,需要导入recyclerview包。

导入方式:File —> Project Structure —> Dependencies —>app 。点击 ➕ 选中第一个 Liberty Dependency

就会弹出窗口(下图),在这里输入recyclerview 搜索就可以导包了

在这里插入图片描述

Nine-Patch

导完包后,我们还需要在了解一个工具,Nine-Patch图片制作,这里将不对工具做扩展,直接看看是怎么使用的吧

D:\JS\typora\笔记图片存储\image-20210412005635086.png

然后 就可以使用了,通过拉伸上下左右的边框,来控制需要放置的内容。简单理解就是,想把内容放置在那个位置,就通过拉伸解决。下图粉色的就是通过拉伸得到的图片,并不会对原图片产生影响,我们只是通过这种方式,来呈现想要输出的内容。粉色的就是显示内容的地方。

D:\JS\typora\笔记图片存储\image-20210412010110117.png

干活

接下来我们正式编写代码,实现聊天界面

修改activity_main.xml中的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#d8e0e8">
    <androidx.recyclerview.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_height="wrap_content"
            android:layout_weight="1"
            android:hint="Type something here"
            android:maxLines="2"/>
        <Button
            android:id="@+id/send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Send"/>
    </LinearLayout>
</LinearLayout>

相信这些代码都是很常见的,这里就不在做过多啰嗦的解释

新建msg_item.xml,编写R

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值