Home Database Mysql Tutorial Android开发入门教程--Android应用程序结构分析

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

Jun 07, 2016 pm 04:01 PM
android Getting Started Tutorial app develop

一、新建HelloWorld项目: 1、打开Eclipse,点击“File”-New-Project-Android Application Project: 在弹出的“New Android Application”窗体中输入相应的应用名称、项目名称、包名称,并选择相应的SDK版本和应用主题: 选择项目保存位置,一路“next”完成

一、新建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 }
Copy after login
复制代码

从代码中可以看到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 }
Copy after login
复制代码

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

layout文件:res/layout/activity_main.xml – 布局文件,双击activity_main.xml会进入可视化编辑界面,在这里你可以根据需要选择相应的控件:

\

也可以点击红框部分进入文本编辑界面直接写对应控件的代码(从截图代码文件可以看到该layout由一个相对布局和一个文本框组成):

\

AndroidManifest文件:在应用程序目录截图中倒数第四个可以看到一个AndroidManifest.xml文件,它是应用程序的配置文件包含在每个安卓应用程序中,它向系统描述了本程序所包括的组件、所实现的功能、所能处理的数据、要请求的资源等,可以近似看做网站中的Web.conig文件,同样它也可以由可视化编辑器或文本编辑器编辑:

\

\

Android.jar文件:Android.jar内部常用包作用概述,如下图所示:

\

可以看到Android.jar里面包含了很多包,常见包的作用如下:

android.app-----------提供高层的程序模型、提供基本的运行环境
android.content-------包含各种的对设备上的数据进行访问和发布的类
android.database------通过内容提供者浏览和操作数据库
android.graphics-------底层的图形库,包含画布,颜色过滤,点,矩形,可以将他们直接绘制到屏幕上.
android.location-------定位和相关服务的类
android.media---------提供一些类管理多种音频、视频的媒体接口
android.net------------提供帮助网络访问的类,超过通常的java.net.* 接口
android.os-------------提供了系统服务、消息传输、IPC 机制
android.opengl--------提供OpenGL 的工具
android.provider-------提供类访问Android 的内容提供者
android.telephony-----提供与拨打电话相关的API 交互
android.view-----------提供基础的用户界面接口框架
android.util------------涉及工具性的方法,例如时间日期的操作
android.webkit---------默认浏览器操作接口
android.widget---------包含各种UI 元素(大部分是可见的)在应用程序的屏幕中使用

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:23 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Sep 11, 2024 am 06:37 AM

OnLeaks has now partnered with Android Headlines to provide a first look at the Galaxy S25 Ultra, a few days after a failed attempt to generate upwards of $4,000 from his X (formerly Twitter) followers. For context, the render images embedded below h

IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size Sep 07, 2024 am 06:35 AM

Alongside announcing two new smartphones, TCL has also announced a new Android tablet called the NXTPAPER 14, and its massive screen size is one of its selling points. The NXTPAPER 14 features version 3.0 of TCL's signature brand of matte LCD panels

Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Sep 07, 2024 am 06:39 AM

The Vivo Y300 Pro just got fully revealed, and it's one of the slimmest mid-range Android phones with a large battery. To be exact, the smartphone is only 7.69 mm thick but features a 6,500 mAh battery. This is the same capacity as the recently launc

Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Sep 12, 2024 pm 09:21 PM

Samsung has not offered any hints yet about when it will update its Fan Edition (FE) smartphone series. As it stands, the Galaxy S23 FE remains the company's most recent edition, having been presented at the start of October 2023. However, plenty of

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:22 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Sep 27, 2024 am 06:23 AM

The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

iQOO Z9 Turbo Plus: Reservations begin for the potentially beefed-up series flagship iQOO Z9 Turbo Plus: Reservations begin for the potentially beefed-up series flagship Sep 10, 2024 am 06:45 AM

OnePlus'sister brand iQOO has a 2023-4 product cycle that might be nearlyover; nevertheless, the brand has declared that it is not done with itsZ9series just yet. Its final, and possibly highest-end,Turbo+variant has just beenannouncedas predicted. T

See all articles