您的位置:首页 > 移动开发 > Android开发

Android工程分析

2016-03-18 20:41 573 查看
我在上一篇博客中介绍了如何搭建Windows下的Android开发环境,并且创建了一个简单的HelloWorld工程,这篇博客分析上一篇博客的工程,打开在上一篇博客中创建的工程后可以看Android中的工程结构如下



src:用于存放创建Android项目的代码

gen:由eclipse自动生成的,用于保存一些配置信息

Android 4.3:用于存放开发Android应用程序用到的Android SDK

Android Dependencies:用于存放开发Android程序中用到的第三方java库
assets:用于存放比较大的资源文件,比方说mp3、视频文件等,assets中的资源没有资源id
bin:用于存放编译打包后的文件
libs:用于存放开发Android程序中用到的第三方java库
res:用于存放资源文件,存放在此文件夹下的所有资源文件都会生成资源id
drawable:用于存放图片资源
layout:用于存放布局文件,把布局文件通过资源id指定给activity,界面就会显示出该布局文件定义的布局
menu:用于存放定义菜单的样式的xml文件
string.xml:用于存放字符串资源,每个资源都会有一个资源id

代码及配置文件分析(本人已经注释)

MainActivity.java中的代码

//android工程的包名
package com.example.helloworld;

//创建Android工程需要导入的包(类似于C/C++中的头文件)
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

//创建一个继承自Activity的类MainActivity
public class MainActivity extends Activity
{
//重写Activity的onCreate()方法
//oCreate()方法类似于C/C++中的main方法
@Override
protected void onCreate(Bundle savedInstanceState)
{
//调用父类的onCreate()方法
super.onCreate(savedInstanceState);

//加载布局文件
setContentView(R.layout.activity_main);
}

//创建操作菜单
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
//给Activity设置Menu
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}


gen目录下的BuildConfig.java文件

/** Automatically generated file. DO NOT MODIFY */
package com.example.helloworld;

public final class BuildConfig {

//设置程序是否处于调试模式下
//DEBUG = true表示程序处于调试模式下
//DEBUG = false表示程序不能调试
public final static boolean DEBUG = true;
}


gen目录下的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.example.helloworld;

public final class R {
public static final class attr {
}
public static final class dimen {
/**  Default screen margins, per the Android Design guidelines.
*/
public static final int activity_horizontal_margin=0x7f040000;
public static final int activity_vertical_margin=0x7f040001;
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int action_settings=0x7f080000;
}
public static final class layout {
public static final int activity_main=0x7f030000;
}
public static final class menu {
public static final int main=0x7f070000;
}
public static final class string {
public static final int action_settings=0x7f050001;
public static final int app_name=0x7f050000;
public static final int hello_world=0x7f050002;
}
public static final class style {
/**
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.

Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.

*/
public static final int AppBaseTheme=0x7f060000;
/**  Application theme.
All customizations that are NOT specific to a particular API-level can go here.
*/
public static final int AppTheme=0x7f060001;
}
}
说明:R.java文件用于设置资源的id

AndroidManifest.xml文件为应用的配置文件,也可以叫清单文件,AndroidManifest.xml文件中的配置信息如下

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<activity
android:name="com.example.helloworld.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>

</manifest>


AndroidManifest.xm文件说明:

<?xml version="1.0" encoding="utf-8"?>:设置xml文件的版本和编码方式

<manifest xmlns:android="http://schemas.android.com/apk/res/android":xml文件的命名空间package="com.example.helloworld":应用的包名

android:versionCode="1":应用的版本用于判断能否升级

android:versionName="1.0" :应用的版本号,发布应用时给用户看

android:minSdkVersion="8":应用最低要求的SDK版本

android:targetSdkVersion="17" :应用编译时的SDK版本

android:allowBackup="true":表示允许设备备份

android:icon="@drawable/ic_launcher":当在Application节点下就是设置应用程序的图标,当在Activity节点下就是设置界面的图标

android:label="@string/app_name":当在Application节点下就是设置设置应用程序的名称,当在Activity节点下就是设置界面的图标

android:theme="@style/AppTheme" :设置应用程序的主题

特别说明,下面的代码在那个Activity下,该Activity就是程序的启动界面

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: