编写聊天界面
开始工作,首先我们需要用到recyclerview,需要导入recyclerview包。
导入方式:File —> Project Structure —> Dependencies —>app 。点击 ➕ 选中第一个 Liberty Dependency
就会弹出窗口(下图),在这里输入recyclerview 搜索就可以导包了
Nine-Patch
导完包后,我们还需要在了解一个工具,Nine-Patch图片制作,这里将不对工具做扩展,直接看看是怎么使用的吧
然后 就可以使用了,通过拉伸上下左右的边框,来控制需要放置的内容。简单理解就是,想把内容放置在那个位置,就通过拉伸解决。下图粉色的就是通过拉伸得到的图片,并不会对原图片产生影响,我们只是通过这种方式,来呈现想要输出的内容。粉色的就是显示内容的地方。
干活
接下来我们正式编写代码,实现聊天界面
修改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