国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > 互联网 > Android开发入门教程--Android应用程序结构分析

Android开发入门教程--Android应用程序结构分析

来源:程序员人生   发布时间:2014-09-08 12:13:10 阅读次数:2498次
一、新建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文件里面有很多类,每个类里面又有很多变量,这些类和变量在我们添加、删除控件或资源文件(图片、声音等)由开发工具自动帮我们维护的,由它来调用应用程序的各种资源,在代码第一句的注释中也说明了“AUTO-GENERATED FILE.  DO NOT MODIFY”。

layout文件:res/layout/activity_main.xml

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生