Android开发之目录结构

Activity生命周期

  • 1、与一般的JAVA项目一样,src文件夹是项目的所有包及源文件(.java)。

  • 2、gen文件夹中包含了一个R.java,这个文件夹及类是在建立项目时自动生成的,这个文件是只读模式,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.practice;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int ic_launcher=0x7f020000;
    }
    public static final class id {
        public static final int menu_settings=0x7f070002;
        public static final int ok=0x7f070001;
        public static final int show=0x7f070000;
    }
    public static final class layout {
        public static final int activity_main=0x7f030000;
    }
    public static final class menu {
        public static final int activity_main=0x7f060000;
    }
    public static final class string {
        public static final int app_name=0x7f040000;
        public static final int hello=0x7f040003;
        public static final int hello_world=0x7f040001;
        public static final int menu_settings=0x7f040002;
    }
    public static final class style {

        public static final int AppBaseTheme=0x7f050000;

        public static final int AppTheme=0x7f050001;
    }
}

可以看到文件中定义了很多常量,这些常量的名字都与res文件夹中的文件夹名相同,这也说明了R.java是项目中资源索引。利用这个文件我们可以很快地找到要使用的资源。由于这个文件不能手动编辑,所以当在项目中加入了新的资源时,只需要刷新一下该项目,R.java文件便自动生成了所有资源的索引。

  • 3、Android 4.2是项目中要用到的包,这个文件夹在项目建立时自动生成。

  • 4、Android 系统为每个新设计的程序提供了/assets目录,这个目录保存的文件可以打包在程序里。/res 和/assets的不同点是,android不为/assets下的文件生成ID。如果使用/assets下的文件,需要指定文件的路径和文件名。

  • 5、接下来的res文件夹中包含了项目的所有资源,比如高低中分辨率程序图标文件(drawable-hdpi、drawable-ldpi、drawable-mdpi)、布局文件(layout)、常量(values)等。

1) 我们先来看看布局文件activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="18dp"
        android:text="@string/hello" />

</RelativeLayout>

  :线性版面配置,在这个标签中,所有的元素都是按由上到下的顺序排列的。

<RelativeLayout>: 相对布局配置。

  android:orientation:表示这个介质的版面配置方式,其中“vertical”代表从上到下垂直布局,而“horizontal”代表从左到右水平布局。

  android:layout_width:定义当前视图在屏幕上所占的宽度,fill_paent即填充整个屏幕。

  android:layout_height:定义当前视图在屏幕上所占的高度,fill_parent即填充整个屏幕。

  :文本标签,用来显示文字,高度设置“wrap_content”表示本文本标签可根据文本来改变高度。

  android:text:设置TextView要显示的内容,“@string/hello”表示引用String.xml文件中的hello所代表的字符串

2) 下面来看常量的定义(strings.xml文件):

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

    <string name="app_name">practice</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="hello">hello</string>

</resources>
  • 6、接下来的AndroidManifest.xml文件,它是每个Android项目所必须的,是整个应用的全局描述文件。文件说明了应用的名称、所使用的图标、以及包含了该项目中所有使用的Activity、Service、Receiver等组件,该文件中代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.practice"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.practice.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>

  .:根节点,描述了package中所有的内容

  xmlns:android:包含命名空间的说明,该命名空间使得Android中各种标准属性能在文件中使用。

  Package:声明应用程序包。

  android:versionCode:该应用程序版本代号

  android:versionName:该应用程序版本名称

  uses-sdk:该应用程序所使用的SDK版本

  :包含package中application级别组件声明的根节点。此元素也可包含application的一些全局和默认的属性,如标签、icon、主题、必要的权限等。一个manifest中至多包含一个此元素

  android:icon:应用程序图标

  android:label:应用程序名

  Activity:Activity是用户打开的一个应用程序的初始页面,大部分被使用到的其他页面也由不同的Activity所实现。每个Activity必须有一个标记对应,无论它给外部使用或是只用于自己的package中。为了支持运行时查找Activity,可包含一个或多个元素来描述Activity所支持的操作。

  android:name:应用程序默认启动的Activity。

  intent-filter:声明了指定的一组组件支持的Intent值,从而形式了IntentFilter。除了能在此元素下指定不同类型的值,属性也能放在这里来描述一个操作所需的唯一标签、icon和其他信息。

  action:组件支持的Intent action

  category:组件支持的Intent Category。这里指定了应用程序默认启动的Activity。

  • 7、project.properties文件:

  记录项目中所需要的环境信息,比如Android的版本等,代码中的注释已经把project.properties解释得很清楚了:

# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

# Project target.
target=android-17
  • 8、proguard-project.txt文件

这个文件是混淆代码的脚本配置文件.

猜你喜欢

转载自xuantan.iteye.com/blog/1748929