首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android应用程序项目构造详解

2012-12-19 
Android应用程序项目结构详解在展开的文件夹层中,”src”、”Android Library”、”assets”、”res”与”AndroidManif

Android应用程序项目结构详解

在展开的文件夹层中,”src”、”Android Library”、”assets”、”res”与”AndroidManifest.xml”同属一层,放置在”\src”里的为主程序、程序类(class);放置在”\res”里的为资源文件(Resource Files),如程序ICON图标、布局文件(\layout)与常数(\values)。

以此TestAndroid程序为例,主程序为”act.java”,其内容与一般Java程序格式相类似:

查看源代码打印帮助01package com.gandl; 02??03import android.app.Activity; 04import android.os.Bundle; 05??06public class Act extends Activity { 07??08??public void onCreate(Bundle savedInstanceState) { 09??10????super.onCreate(savedInstanceState); 11????setContentView(R.layout.main); 12??13??} 14}

主程序里可看见Act类继承自Activity类,在类中重写了onCreate() 方法,在方法内以setContentView() 来设置这个Acvitity要显示的布局(R.layout.main),使用布局配置”\layout\main.xml”,布局文件是以XML格式编写的,内容如下:

查看源代码打印帮助01<?xml version="1.0" encoding="utf-8"?> 02<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"03????android:orientation="vertical"04????android:layout_width="fill_parent"05????android:layout_height="fill_parent"06????> 07<TextView08????android:layout_width="fill_parent"09????android:layout_height="wrap_content"10????android:text="@string/hello"11????/> 12??13</LinearLayout>

布局配置中设置了一个TextView TAG,用以配置文本标签Widget,其内部设置的android:text属性,则是要显示的文字内容,引用”@string”里的hello字符串常数。

查看”values/strings.xml”字符串常数设置如下:

查看源代码打印帮助1<?xml version="1.0" encoding="utf-8"?> 2<resources> 3????<string name="hello">Hello World, act!</string> 4????<string name="app_name">机器人测试</string> 5</resources>

其中”hello”字符串变量的内容为”Hello World, act”,这即是TestAndroid程序显示的文字内容了。

Android应用程序有以下三种类型:

前端Activity(Foreground Activities)。

后台服务(Background Services)。

间隔执行Activity(Intermittent Activities)。

前端Activity就如同这个TestAndroid一样,运行在手机前端程序中;后台服务可能是看不见的系统服务(System Service)、系统Broadcast(广播信息)与Receiver(广播信息)接收器);间隔执行Activity则类似如进程(Threading)、Notification Manager等等。

每一个项目都有一个”AndroidManifest.xml”设置文件,里头包含这个Android应用程序具有哪些Activity、Service或者Receiver,先来看看Hello World制作好的”AndroidManifest.xml”设置文件的内容描述:

01<?xml version="1.0" encoding="utf-8"?> 02<manifest xmlns:android="http://schemas.android.com/apk/res/android"03??????package="com.gandl"04??????android:versionCode="1"05??????android:versionName="1.0"> 06????<application android:icon="@drawable/icon" android:label="@string/app_name"> 07????????<activity android:name=".act"08??????????????????android:label="@string/app_name"> 09????????????<intent-filter> 10????????????????<action android:name="android.intent.action.MAIN" /> 11????????????????<category android:name="android.intent.category.LAUNCHER" /> 12????????????</intent-filter> 13????????</activity> 14??15????</application> 16????<uses-sdk android:minSdkVersion="5" /> 17??18</manifest>

热点排行