智能终端软件设计及开发实验二.docx
《智能终端软件设计及开发实验二.docx》由会员分享,可在线阅读,更多相关《智能终端软件设计及开发实验二.docx(14页珍藏版)》请在冰豆网上搜索。
智能终端软件设计及开发实验二
昆明理工大学信息工程与自动化学院学生实验报告
(2013—2014学年第1学期)
课程名称:
智能终端软件设计及开发开课实验室:
信自楼444机房2013年11月4日
年级、专业、班
计科101
学号
2010
姓名
李力
成绩
实验项目名称
Android用户界面设计与开发
指导教师
游进国
教师评语
该同学是否了解实验原理:
A.了解□B.基本了解□C.不了解□
该同学的实验能力:
A.强□B.中等□C.差□
该同学的实验是否达到要求:
A.达到□B.基本达到□C.未达到□
实验报告是否规范:
A.规范□B.基本规范□C.不规范□
实验过程是否详细记录:
A.详细□B.一般□C.没有□
教师签名:
年月日
一、上机目的及内容
1.上机内容
Android用户界面设计与开发。
请参考下图中界面控件的摆放位置,分别使用线性布局、相对布局和绝对布局实现用户界面,并对比各种布局实现的复杂程度和对不同屏幕尺寸的适应能力。
然后选择一种布局方式,在程序中添加4个按钮的事件处理函数,使用LogCat将捕获的按钮事件显示在Eclipse的控制台中。
2.上机目的
(1)学习并熟悉Android用户界面;
(2)能够基于Android平台初步进行用户界面设计与开发。
二、实验原理及基本技术路线图(方框原理图或程序流程图)
使用Eclipse建立一个Android程序;
分别使用线性布局、相对布局和绝对布局实现用户界面进行界面布局;
选择一种布局方式,在程序中添加4个按钮的事件处理函数,使用LogCat将捕获的按钮事件显示在Eclipse的控制台中。
线性布局
线性布局是最简单,Android开发者使用得最多的布局类型之一,开发者用它来组织你们的用户界面上的控件。
线性布局的作用就像它的名字一样:
它将控件组织在一个垂直或水平的形式。
当布局方向设置为垂直时,它里面的所有子控件被组织在同一列中;当布局方向设置为水平时,所有子控件被组织在一行中。
(1)线性布局的重要特性和属性
◆方向属性(必须),取值可以是vertical或horizontal(类:
LinearLayout)
◆对齐属性(可选),控制子控件在线性布局中如何排列和显示(类:
LinearLayout)
◆layout_weight属性(可选,应用到每个子控件)指定每个子控件在父级线性布局中的相对重要性(类:
)
此外,通用的ViewGroup-style属性也应用到线性布局。
这些属性包括:
◆通用布局参数如layout_height(必须)和layout_width(必须)(类:
◆边距布局参数如margin_top,margin_left,margin_right和margin_bottom(类:
ViewGroup.MarginLayoutParams)
◆布局参数如layout_height和layout_width(类:
(2)给子控件赋权
绝大部分线性布局的属性都是自明性的。
然而layout_weight属性需要一些额外的讨论。
与其它线性布局属性不同,其它属性应用在线性布局视图本身,而这个属性是应用在它的子控件上的。
权值本身应该是一个数字(比如,,,,),如果你把所有子控件的权值加起来等于1(100%)。
子控件的权值控件它在父线性布局中有多“重要”或者留给其多少“空间”。
这一点最好通过例子来说明。
让我们回到我们前面用的彩虹线性布局。
为了允许所有子控件相同地“拉伸”填充线性布局,不管屏幕的大小,我们使用layout_weight来对每个TextView赋予相对权值。
因为有7种我们想赋相同权值的颜色,我们将1除以7大约得到.然而,既然我们想要权值之和最后为1,因此5个控件权值设为,另外两个为——一个微小的区别使得总和刚刚好为1,但是这对于第一个和最后一个控件来说还是很明显的。
当屏幕上有足够空间来正确的展示所有控件的时候,这个权值技巧很有效。
那就是说,当空间很紧的时候,权值属性可能会被其它因素覆盖,比如视图裁剪或者在TextView下试图不环绕文本。
当我们改变布局文件以包含相似水平布局(layout_height还是设置为fill_parent)的时候,这变得很明显。
Android程序用户界面使用布局来定义,而线性布局是最基本的布局类型之一。
线性布局允许子控件被组织在一行或一行(水平)或一列(竖直)上。
子控件的位置可以使用对齐和权值属性进一步设置。
相对布局
除了将控件显示在一行或一列的线性布局,相对布局也是Android用户界面设计使用得很普遍的布局类型。
和其它布局很相似,相对布局可以通过XML布局资源来定义也可以用Java程序来定义。
相对布局的功能就像它的名字表达的一样:
它相对其它控件或父控件本身来组织控件。
(1)相对布局的重要特性和属性
◆用于子控件在父布局中居中的规则,包括:
水平居中,垂直居中,或者两者皆居中。
◆用于子控件在父布局中排布的规则,包括:
在顶部,底部,左,右边缘放置。
◆用于子控件相对于其它子控件排布的规则,包括:
在另一个控件顶,底,左,右边缘放置。
◆用于子控件相对于其它子控件放置的规则,包括:
在另一个控件上面,底下,左边或右边放置。
同样的,通用的ViewGroup-style属性也可以应用于相对布局。
这些属性包括:
◆通用布局参数比如layout_height(必须)和layout_width(必须)(类:
◆边距布局参数比如margin_top,margin_left,margin_right和margin_bottom(类:
ViewGroup.MarginLayoutParams)
布局参数比如layout_height和layout_width(类:
(2)相对布局使用技巧
◆这里是一些使用相对布局的技巧。
◆相对布局的子控件必须有唯一的id属性以使规则正确应用。
◆当心循环规则。
循环规则发生在两个控件具有互相指向的规则时。
如果你在布局设计中使用了循环规则,你将会得到以下错误信息:
IllegalStateException:
CirculardependenciescannotexistinaRelativeLayout(相对布局中不允许存在循环依赖)
◆回忆一下相对布局规则的应用被一次处理是很有用的
◆保持你的相对布局规则最小化。
这减小了循环规则的机率并且使得你的布局更加可维护和灵活。
◆一般地,记住测试一下你的布局设计在横屏和竖屏模式下,以及在不同的屏幕大小和解决方案下是不是符合预期的。
◆使用相对布局代替嵌套线性布局以改进程序性能和响应能力。
Android程序用户界面使用布局来定义,相对布局是用于使得程序屏幕更加灵活和强大的布局类型之一。
相对布局允许子控件相对于其它子控件和相对于父控件(边缘以及水平和垂直居中)来组织。
一旦你掌握了如何使用相对布局的规则,它们可以有非常多的用处,使你能够创建复杂布局,而不需要过多嵌套不同的布局,因此也改进了性能。
绝对布局
绝对布局(AbsoluteLayout)是指一个ViewGroup以绝对方式显示它的子视图(View)元素,即以坐标的方式来定位在屏幕上位置。
这种布局方式很好理解,在布局文件中设置View的坐标,从而绝对地定位。
AbsoluteLayout可以让子元素指定准确的x和y坐标值,并显示在屏幕上。
其中坐标(0,0)为左上角,当向下或向右移动时,坐标值将变大。
AbsoluteLayout没有页边框,允许元素之间互相重叠(尽管不推荐)。
我们通常不推荐使用AbsoluteLayout,除非有正当理由使用它,因为它会使界面代码太过刚性,以至于在不同的设备上可能不能很好地工作。
具体实现
使用Eclipse创建一个名为“004”的Android工程。
编写布局文件“res/layour/”
执行效果如图:
三、所用仪器、材料(设备名称、型号、规格等或使用软件)
1台PC及Eclipse、ADT开发软件
四、实验方法、步骤(或:
程序代码或操作过程)
1.界面布局结果如图,如下
线性布局代码:
android=
xmlns:
tools=
android:
layout_width="match_parent"
android:
layout_height="match_parent"
android:
background="@style/AppBaseTheme"
android:
orientation="vertical"
android:
paddingBottom="@dimen/activity_vertical_margin"
android:
paddingLeft="@dimen/activity_horizontal_margin"
android:
paddingRight="@dimen/activity_horizontal_margin"
android:
paddingTop="@dimen/activity_vertical_margin"
android:
visibility="visible"
tools:
context=".LineMainActivity"
android:
layout_width="match_parent"
android:
layout_height="wrap_content"
android:
orientation="horizontal"
android:
visibility="visible">
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
text="@string/name"/>
android:
id="@+id/editText1"
android:
layout_height="wrap_content"
android:
layout_weight=""/>
android:
layout_width="match_parent"
android:
layout_height="wrap_content"
android:
orientation="horizontal"
android:
visibility="visible">
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
text="@string/age"/>
android:
id="@+id/editText2"
android:
layout_height="wrap_content"
android:
layout_weight=""/>
android:
layout_width="match_parent"
android:
layout_height="wrap_content"
android:
orientation="horizontal"
android:
visibility="visible">
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
text="@string/height"/>
android:
id="@+id/editText3"
android:
layout_height="wrap_content"
android:
layout_weight=""/>
android:
layout_width="match_parent"
android:
layout_height="wrap_content"
android:
orientation="horizontal"
android:
visibility="visible">
相对布局代码如下:
xmlversion=""encoding="utf-8"?
>
android:
layout_width="match_parent"
android:
layout_height="fill_parent"
android:
paddingBottom="@dimen/activity_vertical_margin"
android:
paddingLeft="@dimen/activity_horizontal_margin"
android:
paddingRight="@dimen/activity_horizontal_margin"
android:
paddingTop="@dimen/activity_vertical_margin"
>
android:
id="@+id/label"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_alignBottom="@+id/editText1"
android:
layout_alignParentLeft="true"
android:
layout_alignTop="@+id/editText1"
android:
text="姓名:
"/>
android:
id="@+id/editText1"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_alignParentRight="true"
android:
layout_alignParentTop="true"
android:
layout_marginRight="15dp"
android:
layout_marginTop="10dp"
android:
layout_toRightOf="@+id/label"
android:
ems="10"/>
android:
id="@+id/label2"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_alignLeft="@+id/label1"
android:
layout_below="@+id/editText2"
android:
layout_marginTop="17dp"
android:
text="身高:
"/>
android:
id="@+id/editText2"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_alignLeft="@+id/editText3"
android:
layout_alignRight="@+id/editText1"
android:
layout_below="@+id/editText1"
android:
layout_marginTop="16dp"
android:
ems="10"/>
android:
id="@+id/editText3"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_alignLeft="@+id/editText1"
android:
layout_alignRight="@+id/editText1"
android:
layout_alignTop="@+id/label2"
android:
ems="10"/>
android:
id="@+id/button2"
android:
layout_width="70dp"
android:
layout_height="wrap_content"
android:
layout_alignLeft="@+id/label2"
android:
layout_below="@+id/editText3"
android:
text="添加数据"/>
android:
id="@+id/button3"
android:
layout_width="70dp"
android:
layout_height="wrap_content"
android:
layout_alignBaseline="@+id/button2"
android:
layout_alignBottom="@+id/button2"
android:
layout_toRightOf="@+id/button2"
android:
text="全部显示"/>
android:
id="@+id/button4"
android:
layout_width="70dp"
android:
layout_height="wrap_content"
android:
layout_alignBaseline="@+id/button3"
android:
layout_alignBottom="@+id/button3"
android:
layout_toRightOf="@+id/button3"
android:
text="清除显示"/>
android:
id="@+id/button5"
android:
layout_width="70dp"
android:
layout_height="wrap_content"
android:
layout_alignBaseline="@+id/button4"
android:
layout_alignBottom="@+id/button4"
android:
layout_toRightOf="@+id/button4"
android:
text="全部删除"/>
android:
id="@+id/label1"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_alignBaseline="@+id/editText2"
android:
layout_alignBottom="@+id/editText2"
android:
layout_toLeftOf="@+id/editText2"
android:
text="年龄:
"/>
绝对布局代码如下:
xmlversion=""encoding="utf-8"?
>
android:
layout_width="fill_parent"
android:
layout_height="fill_parent"
>
id="@+id/label"
android:
layout_x="10dip"
android:
layout_y="20dip"
android:
layout_height="wrap_content"
android:
layout_width="wrap_content"
android:
text="姓名:
"/>
android:
id="@+id/entry"
android:
layout_width="246dp"
android:
layout_height="wrap_content"
android:
layout_x="50dip"
android:
layout_y="10dip"/>
android:
id="@+id/label1"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_x="10dp"
android:
layout_y="70dp"
android:
text="年龄:
"/>
android:
id="@+id/entry1"
android:
layout_width="246dp"
android:
layout_height="wrap_content"
android:
layout_x="50dip"
android:
layout_y="60dip"/>
android:
id="@+id/label2"
android:
layout_width="wrap_content"
androi