智能机器人“小昆”的实现(四)布局实现

经过漫长的代码编写,我们终于可以进行布局的编写了。我们再来看一下效果图,如下:

首先可以看到,主布局很简单,上面是一个ListView,底部并排放着一个编辑框和一个发送按钮。然后就是ListView的子项布局,我们发现有两种。一种是发送消息的布局,整体靠右,左边是一个消息框,右边是一个头像,头像下面有昵称。还有一种是服务器返回的消息的布局,整体 靠左,往右依次为头像和消息框,且头像下也有昵称。好了,分析完毕,下面我们就开始编写这个布局吧。

一、素材

这个项目中所用的图片素材,请点击下面的链接,下载”智能机器人素材“即可。

http://pan.baidu.com/disk/home

二、ListView子项布局实现

ListView子项布局有两种,我们分别命名为 item_in.xml(自己发送消息的布局),item_out.xml(服务器端消息的布局)。先编写item_in.xml。代码如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     android:gravity="right">
 7
 8
 9     <LinearLayout
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         android:orientation="horizontal">
13
14
15         <TextView
16             android:id="@+id/tv_inmsg"
17             android:layout_width="wrap_content"
18             android:layout_height="wrap_content"
19             android:background="@drawable/chatto_bg_focused"
20             android:layout_gravity="center_vertical"
21             android:gravity="center_vertical"
22             android:textColor="@color/red"
23             android:textSize="15dp"/>
24
25         <LinearLayout
26         android:layout_width="wrap_content"
27         android:layout_height="wrap_content"
28         android:orientation="vertical">
29
30             <ImageView
31                 android:layout_width="wrap_content"
32                 android:layout_height="wrap_content"
33                 android:src="@drawable/yang2"/>
34             <TextView
35                 android:layout_width="wrap_content"
36                 android:layout_height="wrap_content"
37                 android:layout_gravity="center"
38                 android:text="@string/xiaoyang"/>
39         </LinearLayout>
40
41     </LinearLayout>
42
43
44 </LinearLayout>

再编写 item_out.xml.代码如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6
 7     <TextView
 8         android:id="@+id/tv_out_date"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:layout_gravity="center"
12         android:textColor="@color/green"
13         android:background="@color/gray"
14         android:textSize="20dp"
15        />
16     <LinearLayout
17         android:layout_width="wrap_content"
18         android:layout_height="wrap_content"
19         android:orientation="horizontal">
20
21         <LinearLayout
22         android:layout_width="wrap_content"
23         android:layout_height="wrap_content"
24         android:orientation="vertical">
25
26             <ImageView
27                 android:layout_width="wrap_content"
28                 android:layout_height="wrap_content"
29                 android:src="@drawable/me"/>
30             <TextView
31                 android:layout_width="wrap_content"
32                 android:layout_height="wrap_content"
33                 android:layout_gravity="center"
34                 android:text="@string/xiaokun"/>
35         </LinearLayout>
36
37         <TextView
38             android:id="@+id/tv_outmsg"
39             android:layout_width="wrap_content"
40             android:layout_height="wrap_content"
41             android:background="@drawable/chatfrom_bg_focused"
42             android:layout_gravity="center_vertical"
43             android:gravity="center_vertical"
44             android:textSize="15dp"
45             android:textColor="@color/red"/>
46     </LinearLayout>
47
48
49 </LinearLayout>

三、主布局实现

然后实现主布局,代码如下:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:background="@color/white"
 6     android:orientation="vertical">
 7
 8     <ListView
 9         android:id="@+id/listview_msg"
10         android:layout_width="match_parent"
11         android:layout_height="0dp"
12         android:layout_weight="1"
13         android:divider="@null"
14         android:dividerHeight="5dp"
15        >
16
17     </ListView>
18
19     <LinearLayout
20         android:id="@+id/id_lay"
21         android:layout_width="match_parent"
22         android:layout_height="wrap_content"
23         android:orientation="horizontal">
24      <EditText
25         android:id="@+id/et_inmsg"
26         android:layout_width="0dp"
27         android:layout_height="wrap_content"
28         android:layout_gravity="center"
29         android:layout_weight="1"
30        />
31
32      <ImageButton
33         android:id="@+id/btn_send"
34         android:layout_gravity="center"
35         android:layout_width="wrap_content"
36         android:layout_height="wrap_content"
37         android:text="@string/send"
38         android:textSize="25dp"
39         android:src="@drawable/button"
40        />
41
42
43     </LinearLayout>
44
45
46
47
48 </LinearLayout>

对了,不要忘记values下,strings.xml和color.xml的编写。

strings.xml的代码如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3
 4     <string name="app_name">小昆</string>
 5     <string name="action_settings">Settings</string>
 6     <string name="xiaokun">小昆</string>
 7      <string name="xiaoyang">羊羊</string>
 8
 9     <string name="send">发送</string>
10     <string name="info">亲,请写点东西给小昆吧!</string>
11
12 </resources>

color.xml的代码如下:

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3     <color  name ="green">#00cc00</color>
4     <color  name ="gray">#808080</color>
5     <color  name ="red">#cc0000</color>
6      <color  name ="white">#ffffff</color>
7
8 </resources>

好了,至此布局已经都编写完成了。下面就可以进行MainActivity的编写了。

时间: 2024-08-05 23:23:07

智能机器人“小昆”的实现(四)布局实现的相关文章

智能机器人“小昆”的实现(一)项目介绍以及图灵授权认证

一.项目介绍 这个小项目开发了一个叫做“小昆”的图灵智能机器人,它可以陪你聊天,听你抱怨.而且可以在后台设置机器人知识库,打造你的专属智能机器人.我们先看几张运行的效果图,介绍一下项目吧.如下图: 功能说明:其实很简单,只要你在编辑框中输入你想要对机器人小昆说的话,然后点击发送按钮(即那个蝴蝶),就可以愉快的跟小昆聊天了. 原理说明:本项目智能机器人小昆的实现主要是调用了第三方的API,即图灵机器人的接口.而其他的模块则是android原生的代码编写.并不算是复杂. 通过本项目你可以学习到: (

智能机器人“小昆”的实现(三)实体类的实现

在上一篇文章中,我们实现了获取服务器返回数据的工具类.没有读过的朋友可以点击下面链接: http://www.cnblogs.com/fuly550871915/p/4852568.html 这一篇文章要在上一篇的基础上,实现两个重要的实体类,一个是ChatMsg,用来封装数据从而实现标准的消息格式:一个是ResultMsg,用来封装从服务器端返回的数据. 一.准备工作 在这里,解析json字符串,我们打算使用第三方包GSON.首先点击下面的地址,下载该包. http://code.google

智能机器人“小昆”的实现(二)获取服务器数据工具类的编写及测试

没有取得图灵机器人认证的朋友可以看上一篇,点击下面的地址即可: http://www.cnblogs.com/fuly550871915/p/4852148.html 已经取得认证的朋友,可以继续往下做了.下面就开始我们的实际代码编写.在这篇文章中,我们要实现通过调用图灵机器人API获取返回的数据的功能.而且搭建相应的测试环境,看看我们这个功能实现的到底正确不正确. 一.获得服务器返回数据的工具类的实现 主要是android中的简单的网络编程知识的运用.代码如下: 1 package com.f

慕课网《Android智能机器人“小慕”的实现》项目上手操作与代码解读【1】

慕课网是我所找到的免费教程网站之一(http://www.imooc.com/),上面的项目设计前端.后端.移动开发等很多领域,个人感觉如果对编程感兴趣的话,慕课网绝对是上手项目的最佳选择.这个网站给出的一些项目都很新颖,比一些书上给的不知道沿用了多少年的项目新颖多了,而且在学习的时候如果发现问题可以给老师留言,然后老师都会一一回复.并且每一个项目都会附有源代码及一些资源,如果跟不上老师进度的话可以自己down下来仔细研究.好了,对慕课网我就介绍这么多吧. 因为本人曾经在大三暑假的时候自学过一点

小程序开发之页面布局

Flex布局又称弹性布局,在小程序开发中比较适用.因此将Flex布局相关属性整理如下,搞清楚了这个布局,小程序开发的页面布局就不在话下了. 网页布局(layout)是CSS的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现. 2009年,W3C提出了一种新的方案--Flex布局,可以简便.完整.响应式地实现各种页面布局.目前,它已经得到了所有浏览器的支持,这意味着,现

Thinkphp入门 四 —布局、缓存、系统变量 (48)

原文:Thinkphp入门 四 -布局.缓存.系统变量 (48) [控制器操作方法参数设置] http://网址/index.php/控制器/操作方法 [页面跳转] [变量调节器] Smarty变量调节器 TP变量调节器:普通的php函数 (count  strlen   str_replace) 定义:前者的输出,是后者的输入 [子模板包含] 当前模块彼此包含 <include  file=”模板名称”  /> [使用布局layout] 1. 开启布局,配置变量信息config.php 2.

Cocos2dx 小技巧(十四)ScrollView实现缩放效果

这阶段心绪比較乱,所以这篇开头就不扯淡了.(谁说大姨夫来了我跟谁急!~~)说到大姨夫我突然想到英雄联盟有个美女讲解叫伊芙蕾亚,她的堂弟ID居然叫:姨夫累呀,好笑吧(呵呵,有点冷~~额,我都说不扯淡了).------------前天有个网友问我一些关于scrollView的使用方法,因为在QQ上实在讲不清,所以就利用晚上的时间写这篇博客出来了.本篇要实现的功能是用scrollView 拖动对象时,对象移动到某个固定范围会有放大.缩小的效果.以下開始.在进入正题前我先简短的介绍下scrollView

小谷实战Jquery(四)--标签页效果

这两天完成了实战四五六的例子,实例四是标签页的实现方法,实例五是级联菜单下拉框,实例六是窗口效果,都是web层常用的效果.越到后面越发觉得技术这东西,就是一种思路的展现,懂了要实现效果的来龙去脉,代码就是表达的一种工具,后台展示的是逻辑,前台展现的是图形. 说一下这个标签页吧,第一个标签由两部分组成,鼠标移到上面标签上,下面对应显示相应的内容.借助CSS实现标签和内容相融合的效果.这次我们先看最终效果. HTML: <span style="font-size:18px;">

微信小程序把玩(四十一)canvas API

原文:微信小程序把玩(四十一)canvas API 绘图是每个移动应用必备的技术,基本上和Android,IOS,等移动开发都是相同的,创建个上下文,给你个画布再上画,官网给的小例子都比较全了自己去看吧,drawImage时没有反应不知道是BUG还是电脑不能测试待定,http://wxopen.notedown.cn/api/api-canvas.html 屏幕就像是数学上的坐标轴,且在第四象限,以屏幕左上角为圆点,X轴向右为正向左为负,Y轴向下为正向上为负(这点和数学上相反的)以圆点为基点画个