Android学习笔记(二)App工程文件分析


App工程文件分析

关于如何创建一个最简单的Android App请参照链接:

《 Android学习笔记(一)环境安装及第一个hello world 》 http://www.phpstudy.net/article/52593.htm

创建完的工程文件如下图所示,本文对一些主要的文件进行分析。


src文件分析

App源文件如图:


打开源文件 MainActivity.java 可看到如下代码:


源码主要功能如下:

App源文件目录

package com.example.firstapp; 

导入App所需的类

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 

MainActivity继承自Activity

public class MainActivity extends Activity

重载onCreate方法,使用布局文件初始化Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
} 

重载onCreateOptionsMenu方法,使用布局文件初始化Menu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
}

gen与res文件

gen文件夹下R.java文件在创建工程时自动创建,为只读文件,定义了项目所有资源的索引,里面的每个静态类都与一个资源对应。

例如:

1. 类drawable与res中包含drawable字样的文件夹关联

2. 类layout与res中layout文件夹关联

3. 类menu与res中menu文件夹关联

res文件夹下是App所使用的资源文件,其中:

1. drawable与icon相关

2. layout与布局相关

3. menu与menu布局相关

4. value字样的定义了项目配置中使用的值

举例: 界面中的文字

value的文件夹下的strings.xml文件中定义了名称为hello_world的字符串,其值为" hello world! "
layout文件夹下的activity_main.xml中定义了Textveiw中的文字为hello_world字符串。
Android Menifest.xml

App的主要配置文件,内容如下:

配置App信息

package="com.example.firstapp"
android:versionCode="1"
android:versionName="1.0"  

配置SDK等级

android:minSdkVersion="8"
android:targetSdkVersion="19"

配置App资源

配置App的图标、名称及主题等,其资源与res文件夹对应。 

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

 
配置App的Activity和App名称

android:name="com.example.firstapp.MainActivity"
android:label="@string/app_name" 

配置App的intent-filter

action

  android:name="android.intent.action.MAIN"
category

  android:name="android.intent.category.LAUNCHER"

最后

以上为App工程文件分析,个人理解,仅供参考。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3