在Eclipse中新建一个Android Project,名称为“AndroidTest”,则项目结构如图:

  1.1、与一般的JAVA项目一样,src文件夹是项目的所有包及源文件(.java)。
  1.2、gen文件夹中包含了一个R.java,这个文件夹及类是在建立项目时自动生成的,这个文件是只读模式,R.java文件是定义该项目所有的资源文件的索引文件。先来看看R.java文件代码:

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
  *
  * This class was automatically generated by the
  * aapt tool from the resource data it found.  It
  * should not be modified by hand.
  */
 package com.lrjs.AndroidTest;
 public final class R {
     public static final class attr {
     }
     public static final class drawable {
         public static final int icnotallow=0x7f020000;
     }
     public static final class layout {
         public static final int main=0x7f030000;
     }
     public static final class string {
         public static final int app_name=0x7f040001;
         public static final int hello=0x7f040000;
     }
 }
   可以看到这里定义了很多常量,这些常量的名字都与res文件夹中的文件夹名相同,这也说明了R.java是项目中资源索引。利用这个文件我们可以很快地找到要使用的资源。由于这个文件不能手动编辑,所以当在项目中加入了新的资源时,只需要刷新一下该项目,R.java文件便自动生成了所有资源的索引。
   1.3、Android 2.2是项目中要用到的包,这个文件夹在项目建立时自动生成。
   1.4、Android 系统为每个新设计的程序提供了/assets目录,这个目录保存的文件可以打包在程序里。/res 和/assets的不同点是,android不为/assets下的文件生成ID。如果使用/assets下的文件,需要指定文件的路径和文件名。
   1.5、接下来的res文件夹中包含了项目的所有资源,比如高低中分辨率程序图标文件(drawable-hdpi、drawable-ldpi、drawable-mdpi)、布局文件(layout)、常量(values)等。
   1)我们先来看看布局文件main.xml:

 <linearlayout xmlns:android="<A href=" http:="" schemas.android.com="" apk="" res="" android"="" style="word-wrap: break-word; ">http://schemas.android.com/apk/res/Android"
     android:orientatinotallow="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     >
 <textview  
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello"
     />

   :线性版面配置,在这个标签中,所有的元素都是按由上到下的顺序排列的。
   android:orientation:表示这个介质的版面配置方式,其中“vertical”代表从上到下垂直布局,而“horizontal”代表从左到右水平布局。
   android:layout_width:定义当前视图在屏幕上所占的宽度,fill_paent即填充整个屏幕。
   android:layout_height:定义当前视图在屏幕上所占的高度,fill_parent即填充整个屏幕。
   :文本标签,用来显示文字,高度设置“wrap_content”表示本文本标签可根据文本来改变高度。
   android:text:设置TextView要显示的内容,“@string/hello <mailto:“@string/hello>”表示引用String.xml文件中的hello所代表的字符串
   2)下面来看常量的定义(String.xml文件):


     Hello World, AndroidTest!
     AndroidTest

   这个文件很简单,就定义了两个字符串资源,R.java中的:
 public static final class string {
         public static final int app_name=0x7f040001;
         public static final int hello=0x7f040000;
     }
   这里的app_name和hello即指向String.xml文件中的两个字符串资源
   那么如何在程序中使用我们定义的这些资源文件呢?首先通过Context的getResources实例化一个Resources对象,然后通过Resources的getString方法取得指定索引的字符串,代码如下:
 Resources r=this.getContext().getResources();
 String appname=((String)r.getString(R.string.app_name));
 String hello=((String)r.getString(R.string.hello));
   1.6、接下来的AndroidManifest.xml文件则包含了该项目中所有使用的Activity、Service、Receiver,该文件中代码如下:

 <manifest xmlns:android="<A href=" http:="" schemas.android.com="" apk="" res="" android"="" style="word-wrap: break-word; ">http://schemas.android.com/apk/res/android"
       package="com.lrjs.AndroidTest"
       android:versinotallow="1"
       android:versinotallow="1.0">
     
     
         <activity android:name=".AndroidTest" 
                   android:label="@string/app_name">
             
                 
                 
             
         
     

   :根节点,描述了package中所有的内容
   xmlns:android:包含命名空间的说明,该命名空间使得Android中各种标准属性能在文件中使用。
   Package:声明应用程序包。
   android:versionCode:该应用程序版本代号
   android:versionName:该应用程序版本名称
   uses-sdk:该应用程序所使用的SDK版本
   :包含package中application级别组件声明的根节点。此元素也可包含application的一些全局和默认的属性,如标签、icon、主题、必要的权限等。一个manifest中至多包含一个此元素
   android:icon:应用程序图标
   android:label:应用程序名
   Activity:Activity是用户打开的一个应用程序的初始页面,大部分被使用到的其他页面也由不同的Activity所实现。每个Activity必须有一个标记对应,无论它给外部使用或是只用于自己的package中。为了支持运行时查找Activity,可包含一个或多个元素来描述Activity所支持的操作。
   android:name:应用程序默认启动的Activity。
   intent-filter:声明了指定的一组组件支持的Intent值,从而形式了IntentFilter。除了能在此元素下指定不同类型的值,属性也能放在这里来描述一个操作所需的唯一标签、icon和其他信息。
   action:组件支持的Intent action
   category:组件支持的Intent Category。这里指定了应用程序默认启动的Activity。
   1.7、default.properties文件:
   记录项目中所需要的环境信息,比如Android的版本等,代码中的注释已经把default.properties解释得很清楚了:
 # This file is automatically generated by Android Tools.
 # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
 #
 # This file must be checked in Version Control Systems.
 #
 # To customize properties used by the Ant build system use,
 # "build.properties", and override values to adapt the script to your
 # project structure.
 # Project target.
 target=android-8
   1.8、proguard.cfg文件
   这个文件是混淆代码的脚本配置文件,我目前很少用啦。
   好了,至此差不多已经分析完毕,最后我们来看看源文件AndroidTest.java中的代码:
 package com.lrjs.AndroidTest;
 import android.app.Activity;
 import android.os.Bundle;
 public class AndroidTest extends Activity {
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
     }
 }


  主程序AndroidTest类继承自Activity类,重写了void onCreate(Bundle savedInstanceState)方法。在onCreate方法中通过setContentView(R.layout.main)调用布局文件layout/main.xml来显示。
   运行后显示如下: