第三章 制作Nine-Patch图片与编写精美聊天界面

一、制作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="@+
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值