聊天页面实战
1 9-Patch图片的制作
准备一对气泡图,如图(画图工具里面画就好了)
如果我们直接把图片放到布局里面拉伸,会出现这样的结果
就很丑,需要使用AS工具把图片转换为一种被特殊处理过的png图片——9-Patch图片
将准备好的气泡图放进项目中,app/src/main/res/drawable-xxhdpi 目录下,然后右键图片,选择Create 9-Patch file,保存到当前目录,这时会得到一张.9.png格式的图片,点开编辑。
然后对其进行处理,处理方式很简单:
上边框和左边框绘制的部分表示当图片需要拉伸时就按这个部分形成的矩形来拉伸
下边框和有边框绘制的部分表示图片里的内容允许被放置在这个矩形中
通过左键选择边框,Shift加左键撤销选择
如图
同样的方式,处理一下白色的聊天框
布局效果如图
2 加RecyclerView依赖
我们需要使用到RV,所以需要在app/build.gradle中加入依赖
3 编写主界面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/recyclerView"
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/inputText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Type something here"
android:maxLine