常见布局
1.相对布局( RelativeLayout)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp" >
<!-- 在指定控件的右边、左边、上边或者下边 -->
<TextView
android:layout_toRightOf="@id/控件id名"
android:layout_toLeftOf="@id/控件id名"
android:layout_above="@id/控件id名"
android:layout_below="@id/控件id名"
/>
<!-- 在父窗体中居中、水平居中、或者竖直居中 -->
<TextView
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
<!-- 与父窗体上边、下边、左边或者右边对齐 -->
<TextView
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
/>
<!-- 与指定控件的左边、右边、上边或者下边对齐 -->
<TextView
android:layout_alignLeft="@id/控件id名"
android:layout_alignRight="@id/控件id名"
android:layout_alignTop="@id/控件id名"
android:layout_alignBottom="@id/控件id名"
/>
<!-- 距离指定控件外边距有xx距离 -->
<TextView
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
</RelativeLayout>
1.1 px与dp和sp之间的关系
(1)dp:像素密度的比值,是与HVGA相比较的,为了保证不同大小的屏幕中组件出现的位置是相同的
(2)sp:文字的单位,便于文字的缩放,并且不会产生锯齿,与dp大小相同
2.线性布局( LinearLayout)
特点:排列方式自上而下或者自左向右
<?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:orientation="vertical/horizontal" 竖直或者水平排列 -->
</LinearLayout>
使用:
(1)线性布局与相对布局经常组合使用
(2)Android4.0以后,在系统【Settings】==>【Developer options】==>勾选【show layout bounds】会显示布局的边界线,便于程序员观看布局方式
3.表格布局( TableLayout)
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- 表示表格中的一行
android:layout:weight————表示的是权重,权重即为比例、份数。此例子两个表格各站一行的1份和2份。不过要设置width为0dp
android:gravity="left/center/right"————是中心所在位置
-->
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:gravity="right"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="卫宫士郎"
/>
<TextView
android:gravity="center"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="远坂凛"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="卫宫士郎"
/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="远坂凛"
/>
</TableRow>
</TableLayout>
4.绝对布局( AbsoluteLayout)
用途:已经被废弃掉的布局方式,不过游戏常使用这种布局方式
特点:会为在此布局中的全部控件添加一个【layout_x="xxdp"】和一个【layout_y="xxdp"】这两个值,指定控件的具体坐标
5.帧布局( FrameLayout)
特点:帧布局的画面是后一个覆盖前一个的结构,可以一层一层的显示出来
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="我是视频,现在正在播放,点击一下就会暂停"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<Button
android:id="@+id/btPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"
android:onClick="play"
/>
<Button
android:id="@+id/btPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停"
android:onClick="pause"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
>
<ImageView
android:id="@+id/ivPic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:visibility="invisible"
/>
</RelativeLayout>
</FrameLayout>
测试
1.测试相关知识
1.1 根据是否知道源代码分为2种
(1)黑盒测试:测试时不知道源码,只关心程序执行过程和结果。一般这种测试员思维方式与众不同,不按正常流程作业,如发现游戏BUG
(2)白盒测试:根据源码写测试代码与测试用例(测试方法)来完成测试
1.2 根据测试粒度可分为3种
(1)方法测试:function test ===> 测试某些方法
(2)单元测试:unit test ===> 测试几个方法组成的单元
(3)集成测试:integration ===> 测试程序的几个模块
1.3 根据测试的次数分为2种
(1)冒烟测试:smoke test ===> android 猴子,可在Android命令行界面中运行猴子来对手机某些软件进行暴力测试(暴力测试)
(2)压力测试:pressure test ===> 100万个用户登录,100万个http请求
2.Android下的Junit测试框架
出现意义:因为现在的Java的程序不是运行在JVM中,是将运行在Dalvik虚拟机上,所以像main函数和一般的junit测试将无法运行
2.1 建立Junit测试类的步骤
(1)测试类需要继承AndroidTestlase类
(2)写好测试方法运行时,会把应用程序打包装到手机上,给手机发送一些指令,所以需要定义你的指令
(3)在清单文件中添加指令,添加具体如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.heima.helloworld.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<!-- 需要添加的第一个节点
android:name=""android.test.InstrumentationTestRunner"————指令集名称,固定不变的
android:targetPackage="测试所在app应用程序的包名"
-->
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.heima.helloworld" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<!-- 需要添加的第二个节点,要用的到函数库,书写格式也是固定不变的 -->
<uses-library android:name="android.test.runner" />
</application>
</manifest>
(4)这是Junit运行测试类即可运行,过程是应用程序打包安装到Android虚拟机中,然后Dalvik解析指令集向工程发送回来信息,测试得以运行
2.2 简单建立清单文件指令集方法
在eclipse中新建工程【AndroidTest Project】,并选择要测试的工程,然后你会发现新建的测试工程的清单文件中已经自动添加好了指令集,你只需复制粘贴即可
Android常见类
1.Context抽象类
位置:android.content.Context包
功能:上下文类,是Activity类的父类体系中最大的父类,获取Activity对象的一些属性信息,类如缓存文件位置、存储文件位置
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | MODE_PRIVATE | 私有权限 | |||||||||
int | MODE_WORLD_READABLE | 可读权限 | |||||||||
int | MODE_WORLD_WRITEABLE | 可写权限 | |||||||||
int | MODE_WORLD_WRITEABLE | 可写权限 | |||||||||
int | MODE_APPEND | 可添加的权限,不覆盖源文件而是在文件内容基础之上添加内容。对于第三者与私有权限相同 |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
abstract File |
getCacheDir()
返回值为【
/data/data/包名/cache】的file对象
| ||||||||||
abstract File |
getFilesDir()
返回值为【
/data/data/包名/files】的file对象
| ||||||||||
abstract FileInputStream |
openFileInput(
String name)
获得字节读取流,并且关联了【
/data/data/包名/files】路径,所以直接输入此路径下的文件名作为参数name即可
| ||||||||||
abstract FileOutputStream |
openFileOutput(
String name, int mode)
获得字节输出流,并且关联了【
/data/data包名/files】路径。mode对应四种权限模式,分为私有,公共,可读,可写
| ||||||||||
abstract SharedPreferences |
getSharedPreferences(
String name, int mode)
获取参数对象,
name为保存的文件名,mode为权限值,存储位置为【
/data/data/包名/shared_prefs】,保存的文件类型是一个xml文件
| ||||||||||
abstract PackageManager |
getPackageManager()
获取工程的包管理器
| ||||||||||
abstract String |
getPackageName()
获取工程的包名
| ||||||||||
abstract AssetManager |
getAssets()
获取资源管理器
|
1.1 Linux文件权限详解
明确:Linux是靠着10字符长的字符串来定义文件权限的
(1)位置0:【-】代表文件 【d】代表目录
(2)位置1-3:表示当前用户对此文件的权限,【r】代表可读,【w】可写,【x】代表可执行,【-】代表不能
(3)位置4-6:表示当前用户所在组中所有成员对此文件的权限,【r】代表可读,【w】代表可写,【x】代表可执行,【-】代表不能
(4)位置7-9:表示其他用户对此文件的权限,【r】代表可读,【w】代表可写,【x】代表可执行,【-】代表不能
注意:权限中的后边9位中的【r、w、x】可以表示二进制的1,【-】表示二进制的0,所以【rw- rw- rw-】可以写成【110 110 110】,即【666】
2.Logcat( 日志猫)
存在意义:因为你的应用程序是运行在虚拟机上的,程序出错异常信息是Davlik虚拟机打印的,你根本看不到。所以这时Logcat会通过adb接口取出虚拟机日志缓冲区中的异常日志信息
日志信息分为5个级别:
(1)Verbose:提醒,黑色
(2)Debug:调试,蓝色
(3)Info:信息,绿色
(4)Warn:警告,橙色
(5)Error:错误,红色
2.1 Log终类
位置:android.util.log
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
static int |
d(
String tag,
String msg)
发送一个调试级别的log到logcat中,tag是这个log的一个字符串标识,一般使用所在类的类名
| ||||||||||
static int |
e(
String tag,
String msg)
发送一个错误级别的log到logcat中
| ||||||||||
static int |
i(
String tag,
String msg)
发送一个信息级别的log到logcat中
| ||||||||||
static int |
v(
String tag,
String msg)
发送一个提醒级别的log到logcat中
| ||||||||||
static int |
w(
String tag,
String msg)
发送一个警告级别的log到logcat中
|
3. Environment类
位置:android.os包,这是一个手机环境对象类
内部存储空间:内存【/data/data】
外部存储空间:SD卡【/mnt/sdcard】或者【/sdcard】
写外部存储设备的权限:android.permission.WRITE_EXTERNAL_SOTRAGE
读外部存储设备的权限:android permission.READ_EXTERNAL_SOTRAGE(4.0以后的新特性,当用户在设置中勾选了【对SD卡进行读写保护】)
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
String | MEDIA_BAD_REMOVAL | SD卡被暴力的拔出了 | |||||||||
String | MEDIA_CHECKING | 检查SD卡 | |||||||||
String | MEDIA_MOUNTED | SD卡被挂载了,并且是可读可写的状态 | |||||||||
String | MEDIA_MOUNTED_READ_ONLY | SD卡被挂载了,并且是只读状态 | |||||||||
String | MEDIA_REMOVED | SD卡被拔掉了 |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
static String |
getExternalStorageState()
获取外部存储设备的状态,与该类中的Media各种字段连用,可判断SD是否被挂载
| ||||||||||
static File |
getExternalStorageDirectory()
获取SD卡路径的File对象
| ||||||||||
static File |
getDataDirectory()
获取内部存储空间路径的File对象
|
4.Formatter类
位置:android.text.format包
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
static String |
formatFileSize(
Context context, long number)
对long型的数字转换为数据大小的字符串
|
5.SharedPreferences接口( 参数类)
位置:android.content包
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
abstract boolean |
contains(
String key)
是否包含键
| ||||||||||
abstract SharedPreferences.Editor |
edit()
获取修改对象,此对象可对参数对象进行增删改的操作
| ||||||||||
abstract Map< String, ?> |
getAll()
获取全部参数和值
| ||||||||||
abstract boolean |
getBoolean(
String key, boolean defValue)
通过键获取值,并且只能获取boolean类型的value值,defValue是指当获取的值为空时取的默认值
| ||||||||||
abstract float |
getFloat(
String key, float defValue)
通过键获取值,并且只能获取float类型的value值,defValue是指当获取的值为空时取的默认值
| ||||||||||
abstract int |
getInt(
String key, int defValue)
通过键获取值,并且只能获取int类型的value值,defValue是指当获取的值为空时取的默认值
| ||||||||||
abstract long |
getLong(
String key, long defValue)
通过键获取值,并且只能获取long类型的value值,defValue是指当获取的值为空时取的默认值
| ||||||||||
abstract String |
getString(
String key,
String defValue)
通过键获取值,并且只能获取String类型的value值,defValue是指当获取的值为空时取的默认值 | ||||||||||
abstract Set< String> |
getStringSet(
String key,
Set<
String> defValues)
通过键获取值,并且只能获取Set<String>类型的value值,defValue是指当获取的值为空时取的默认值
|
5.1 SharedPreference.Editor接口
位置:android.content包
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
abstract SharedPreferences.Editor |
clear()
清空编辑器
| ||||||||||
abstract boolean |
commit()
类似数据库的事务管理,要想要把编辑器存储的键值对存入参数对象必须运行此方法
| ||||||||||
abstract SharedPreferences.Editor |
putBoolean(
String key, boolean value)
保存键值对,value为boolean类型
| ||||||||||
abstract SharedPreferences.Editor |
putFloat(
String key, float value)
保存键值对,value为float类型
| ||||||||||
abstract SharedPreferences.Editor |
putInt(
String key, int value)
保存键值对,value为int类型
| ||||||||||
abstract SharedPreferences.Editor |
putLong(
String key, long value)
保存键值对,value为long类型
| ||||||||||
abstract SharedPreferences.Editor |
putString(
String key,
String value)
保存键值对,value为String类型
| ||||||||||
abstract SharedPreferences.Editor |
putStringSet(
String key,
Set<
String> values)
保存键值对,value为Set<String>类型
| ||||||||||
abstract SharedPreferences.Editor |
remove(
String key)
通过键类删除一个键值对
|
6.Xml类
位置:android.util包
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
static XmlPullParser |
newPullParser()
获取Xml的Pull解析器
| ||||||||||
static XmlSerializer |
newSerializer()
获取Xml的序列化器
|
7.XmlSerializer接口
位置:org.xmlpull.v1.XmlSerializer包
功能:Xml文件的序列化器
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
abstract void |
setOutput(
Writer writer)
设置输出字符流
| ||||||||||
abstract void |
setOutput(
OutputStream os,
String encoding)
设置输出的字节流与编码
| ||||||||||
abstract void |
startDocument(
String encoding,
Boolean standalone)
写Xml的文档头,
standalone独立表示的是Xml是否依赖于其他Xml约束文件独立存在
| ||||||||||
abstract XmlSerializer |
startTag(
String namespace,
String name)
写开始标签,
namespace是约束的命名空间
| ||||||||||
abstract XmlSerializer |
text(char[] buf, int start, int len)
写入文本节点
| ||||||||||
abstract XmlSerializer |
text(
String text)
写入文本节点
| ||||||||||
abstract void |
endDocument()
结束文档,把内存中的全部数据写入到关联的Xml文件中
| ||||||||||
abstract XmlSerializer |
endTag(
String namespace,
String name)
结束一个标签
| ||||||||||
abstract void |
flush()
刷新内存到Xml文件中
|
8.XmlPullParser接口
位置:org.xmlpull.v1.XmlPullParser
功能:Xml文件的解析器
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | END_DOCUMENT | 文档的结束节点 | |||||||||
int | END_TAG | 普通的结束节点 | |||||||||
int | START_DOCUMENT | 文档的开始节点 | |||||||||
int | START_TAG | 普通的开始节点 | |||||||||
int | TEXT | 文本节点 |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
abstract int |
getEventType()
获取每一个节点的类型,
| ||||||||||
abstract int |
next()
Pull解析器的指针跳转到下一个节点上,
并且把下一个节点的类型作为返回值返回
| ||||||||||
abstract String |
getName()
获取节点的名称
| ||||||||||
abstract String |
nextText()
获取下一个节点的内容,一般获取文本节点的内容
| ||||||||||
abstract void |
setInput(
Reader in)
设置输入字符流
| ||||||||||
abstract void |
setInput(
InputStream inputStream,
String inputEncoding)
设置输入字节流,并且设置编码
|
8.File类
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
long |
getTotalSpace()
获取最大的空间
| ||||||||||
long |
getUsableSpace()
获取可用空间
|
9.SmsMessage类
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
static SmsMessage |
createFromPdu(byte[] pdu)
创建实例的方法
| ||||||||||
String |
getMessageBody()
获取短信的内容
| ||||||||||
String |
getOriginatingAddress()
获取发送者的号码
|