设为首页 加入收藏

TOP

Android开发入门教程--Android应用程序结构分析(一)
2014-11-24 03:33:43 来源: 作者: 【 】 浏览:11
Tags:Android 开发 入门教程 --Android 应用程序 结构 分析
一、新建HelloWorld项目:

1、打开Eclipse,点击“File”->"New"->"Project"-Android Application Project"":

\

在弹出的“New Android Application”窗体中输入相应的应用名称、项目名称、包名称,并选择相应的SDK版本和应用主题:

\

选择项目保存位置,一路“next”完成项目创建:

\

创建后的项目:

\

在创建后的项目名称上右键单击选择“Run As”->“Android Application”运行刚创建的项目:

\

运行结果:
\

二、应用程序目录结构简析:

1、应用程序目录结构:

\

2、各部分说明:

Activity文件:双击目录中的“MainActivity.java”,可以看到MainActivity的代码:

复制代码
 1 package android.basic.helloandroid;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.view.Menu;
 6 
 7 public class MainActivity extends Activity {
 8 
 9     @Override
10     protected void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.activity_main);
13     }
14 
15     @Override
16     public boolean onCreateOptionsMenu(Menu menu) {
17         // Inflate the menu; this adds items to the action bar if it is present.
18         getMenuInflater().inflate(R.menu.activity_main, menu);
19         return true;
20     }
21 
22 }
复制代码

从代码中可以看到MainActivity继承于Activity类,Activity是Android中的视图部分,负责处理界面显示。在MainActivity里面重写了父类的onCreate方法和onCreateOptionsMenu方法,在重写的onCreate方法里方法setContentView(R.layout.activity_main)给MainActivity设置了要显示的视图R.layout.activity_main,视图由R类寻找并加载(感觉很像mvc,Activity相当于Controller而要显示的layout就相当于具体的页面)。

R文件:在MainActivity的setContentView(R.layout.activity_main)方法中我们用R.layout.activity_main指定了要显示的视图,在应用程序目录结构的截图中可以看到R文件位于gen目录下面,双击显示代码:

复制代码
 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 
 8 package android.basic.helloandroid;
 9 
10 public final class R {
11     public static final class attr {
12     }
13     public static final class drawable {
14         public static final int ic_launcher=0x7f020000;
15     }
16     public static final class id {
17         public static final int menu_settings=0x7f070000;
18     }
19     public static final class layout {
20         public static final int activity_main=0x7f030000;
21     }
22     public static final class menu {
23         public static final int activity_main=0x7f060000;
24     }
25     public static final class string {
26         public static final int app_name=0x7f040000;
27         public static final int hello_world=0x7f040001;
28         public static final int menu_settings=0x7f040002;
29     }
30     public static final class style {
31         /** 
32         Base application theme, dependent on API level. This theme is replaced
33         by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
34     
35 
36             Theme customizations available in newer API levels can go in
37             res/values-vXX/styles.xml, while customizations related to
38             backward-compatibility can go here.
39         
40 
41         Base application theme for API 11+. This theme completely replaces
42         AppBaseTheme from res/values/styles.xml on API 11+ devices.
43     
44  API 11 theme customizations can go here. 
45 
46         Base application theme for API 14+. This theme completely replaces
47         AppBaseTheme from BOTH res/values/styles.xml and
48         res/values-v11/styles.xml on API 14+ devices.
49     
50  API 14 theme customizations can go here. 
51          */
52         public static final int AppBaseTheme=0x7f050000;
53         /**  Application theme. 
54  All customizations that are NOT specific to a particular API-level can go here. 
55          */
56         public static final int AppTheme=0x7f050001;
57     }
58 }
复制代码

从代码中可以看到R文件里面有很多类,每个类里面又有很多变量,这些类和变量在我们添加、删除控件或资源文件(图片、声音等)由开发工具自动帮我们维护的,由它来调用应用程序的各种资源,在代码第一句的注释中也说

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Redis的AOF功能 下一篇kettle系列教程一

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Java 学习线路图是怎 (2025-12-25 15:19:15)
·关于 Java 学习,有 (2025-12-25 15:19:12)
·有没有Java swing教 (2025-12-25 15:19:09)
·Start, Stop, and Di (2025-12-25 14:50:57)
·C语言入门教程:零基 (2025-12-25 14:50:54)