RecyclerView学了这么久,也应该来试一下实战了,这次来试一下做一个聊天界面,做聊天界面需要两张图片来分别做收到信息和发送信息的背景图,是需要制作Nine-Patch图片,做Nine-Patch图片也很简单,直接右键生成然后编辑就行,不过要注意的是,图片的后缀要是png才可以
我们先来看一下效果图
看起来挺不错的,接下来来实现一下
因为我们会用到RecyclerView,所以首先添加一下依赖,打开app/build.gradle添加依赖
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:recyclerview-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
接着来编写一下主页面,修改activity_main.xml
<?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/bac"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/msg_recycle_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_txet"
android:layout_