Android 目录结构分析

移动开发 Android
Android 目录结构及 Android下Camera框架结构,初学者适用 Android 目录结构及 Android下Camera框架结构,初学者适用。与大家分享一下。

1、Android项目目录结构

android项目和java项目一样,,src文件夹是项目的所有包及源文件(.java),res文件夹中包含了项目中的所有资源,比如:程序图标(drawable)、布局文件(layout)、常量(values)。android项目中多了gen文件夹,里面是R.java文件,定义该项目所有资源的索引文件。还有一个android项目必须的AndroidManfest.xml文件。项目结构图如下:

 

1 R.java文件是项目创建的时候自动生成,是只读文件,不能够更改。其代码如下:

  1. /* AUTO-GENERATED FILE.  DO NOT MODIFY.  
  2.  *  
  3.  * This class was automatically generated by the  
  4.  * aapt tool from the resource data it found. It  
  5.  * should not be modified by hand.  
  6.  */  
  7.   package com.hanfeng.demo;  
  8.   public final class R {  
  9.      public static final class attr {  
  10.      }  
  11.     public static final class drawable {  
  12.          public static final int icon=0x7f020000;  
  13.      }  
  14.      public static final class layout {  
  15.         public static final int main=0x7f030000;  
  16.      }  
  17.      public static final class string {  
  18.          public static final int app_name=0x7f040001;  
  19.          public static final int hello=0x7f040000;  
  20.     }  

通过上面代码,我们可以看到这些常量与res文件夹中的文件名字一样,说明了R.java文件存储了该项目中的所有资源索引。R.java文件的存在,方便资源的使用,当我们在项目中加入新资源,只需刷新项目,R.java文件就会自动生成所有资源的索引。分析上面的xml文件:

2 AndroidManfest.xml文件包含了项目中所有使用的Activity、Service、Receiver,代码如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  3.      package="com.hanfeng.demo" 
  4.       android:versionCode="1" 
  5.       android:versionName="1.0"> 
  6.     <uses-sdk android:minSdkVersion="8" />   
  7.     <application android:icon="@drawable/icon" android:label="@string/app_name"> 
  8.         <activity android:name=".AndroidTest" 
  9.                  android:label="@string/app_name"> 
  10.             <intent-filter> 
  11.                 <action android:name="android.intent.action.MAIN" /> 
  12.                <category android:name="android.intent.category.LAUNCHER" /> 
  13.             </intent-filter> 
  14.         </activity>   
  15.     </application> 
  16.  </manifest> 
 分析上面的xml文件:

【xmlns:android】:包含命名空间的声明。xmlns:android="http://schemas.android.com/apk/res/android",使得Android中各种标准属性能够在文件中使用,提供大部分元素的数据。

【package】:声明应用程序包。

【application】:包含package中application级别组件声明的根节点。此元素耶可包含application的一些全局和默认的属性,如标签、icon、主题、必要权限等。一个manifest能够包含零个或一个此元素,不能大于一个。

【android:icon】:应用程序图标。

【android:lebel】:应用程序名字。

【Activity】:用户交互工具。

【android:name】:应用程序默认启动的Activity。

【intent-filter】:声明了指定一组组件支持的Intent值,从而形成IntentFilter。

【action】:组件支持的Intent action 。

【category】:组件支持的Intent Category。指定应用程序默认启动的Activity。

【uses-sdk】: 应用程序所使用的sdk版本。

#p#

3 String.xml 资源文件中的常量定义。代码如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  <resources> 
  3.    <string name="hello">Hello World, AndroidTest!</string> 
  4.    <string name="app_name">AndroidDemo</string> 
  5. </resources> 

从上面的代码可以看出,文件中定义的字符串资源常量"hello"、"app_name"指向了R.java文件中的字符串资源。

下面看如何使用定义的这些资源。我们可以通过Context的getResource实例化一个Resource对象,然后通过Resource的getSting方法获取指定索引的字符串。代码如下:

  1.  Resource r = this.getContext().getResource();  
  2.  String appname = ((String) r.getString(R.string.app_name));  
  3. String hello = ((String) r.getString(R.string.hello));  

4 main.xml布局文件,代码如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello" 
  11.     /> 
  12. </LinearLayout> 

布局参数解析:

【<linearLayout>】:线性版面配置,在这个标签中,所有元件都是按由上到下的排列排成。

【android:orientation】:表示这个介质的版面配置方式是从上到下的垂直地排列其内部视图。

【android:layout_width】:定义当前视图在屏幕上所占的宽度,fill_parent即填充整个屏幕。

【android:layout_height】:定义当前视图在屏幕上所占的高度。

【wrap_weight】:随着文字栏位的不同而改变这个视图的高度或宽度。

5 AndroidTest.java 项目的猪程序文件。代码如下:

  1. package com.hanfeng.demo;  
  2.  
  3.  import android.app.Activity;  
  4.  import android.os.Bundle;  
  5.  
  6.  public class AndroidTest extends Activity {  
  7.     /** Called when the activity is first created. */  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.main);  
  12.     }  

主程序AndroidTest继承自Activity类,重写了void onCreate(Bundle saveInstanceState)方法。在onCreate方法中通过setContentView(R.layout.main)设置Activity要显示的布局文件。

【编辑推荐】

Android开发之旅:Android架构

Android应用程序开发环境的搭建

在Android应用程序中使用Internet数据

Android核心分析之一->分析方法论探讨之设计意图

责任编辑:zhaolei 来源: 网络转载
相关推荐

2018-04-27 10:59:30

Linux目录结构lib

2013-01-16 14:19:03

Android工程目录结构Android开发

2010-04-15 11:47:37

微软活动目录逻辑结构

2013-01-17 15:26:21

Android工程目录结构Android开发

2013-05-23 15:18:13

Android开发移动开发程序目录结构

2011-09-14 16:33:04

2013-10-30 22:50:30

Clouda结构

2011-01-10 10:30:05

linux目录结构

2010-03-09 14:04:28

2014-04-28 16:13:11

Unix目录结构

2012-02-08 09:48:25

开源项目

2010-05-26 19:05:06

SVN库

2010-05-26 19:36:34

SVN目录结构

2010-05-27 10:53:54

SVN目录结构

2010-11-03 16:50:23

DB2目录结构

2013-03-22 17:12:34

Android工程代码结构

2010-11-02 09:56:14

DB2目录结构

2022-07-18 05:59:18

Linux目录结构操作系统

2014-03-06 10:50:59

iOS开发

2010-04-25 23:13:26

活动目录物理结构
点赞
收藏

51CTO技术栈公众号