一、制作Nine-Patch图片
1、准备好一张聊天气泡图片message_left
2、修改activity_main中代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/message_left">
</LinearLayout>
运行程序,发现整张图片被均匀拉伸了,显得很难看,这时我们就可以用9-patch图片来改善这种状况
3、开始制作Nine-Patch图片
①先将message_left图片放到drawable文件夹中
②在AndroidStudio中drawable中找到图片,右键图片,选create 9-patch file 并将create出的文件放在drawable文件夹中
注:第一行代码书中这里是用draw9patch.bat文件制作9-patch图片,然而最新版的Androidstudio已经将这个功能集成进软件中了,所以在Android Sdk目录下的tools文件夹中是找不到draw9patch.bat文件的
③双击create出的.9.png文件,按需求制作9-patch图片,制作好了用这张图替换原图片(将原图删掉即可)就好了
4、再次运行程序
发现图片拉伸的时候只拉伸我们指定的区域了,界面变得更加美观了
二、编写精美聊天界面
1、按上述步骤再制作一个message_right.9.png图片
2、由于之后会用到RecyclerVIew所以先向build.gradle文件中添加库
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
3、制作聊天界面雏形
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#d8e0e8">
<android.support.v7.widget.RecyclerView
android:id="@+id/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="@+