Android工程分析

我在上一篇博客中介绍了如何搭建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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值