自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android 目錄結構分析

移動開發(fā) Android
Android 目錄結構及 Android下Camera框架結構,初學者適用 Android 目錄結構及 Android下Camera框架結構,初學者適用。與大家分享一下。

1、Android項目目錄結構

android項目和java項目一樣,,src文件夾是項目的所有包及源文件(.java),res文件夾中包含了項目中的所有資源,比如:程序圖標(drawable)、布局文件(layout)、常量(values)。android項目中多了gen文件夾,里面是R.java文件,定義該項目所有資源的索引文件。還有一個android項目必須的AndroidManfest.xml文件。項目結構圖如下:

 

1 R.java文件是項目創(chuàng)建的時候自動生成,是只讀文件,不能夠更改。其代碼如下:

  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.   package com.hanfeng.demo;  
  8.   public final class R {  
  9.      public static final class attr {  
  10.      }  
  11.     public static final class drawable {  
  12.          public static final int icon=0x7f020000;  
  13.      }  
  14.      public static final class layout {  
  15.         public static final int main=0x7f030000;  
  16.      }  
  17.      public static final class string {  
  18.          public static final int app_name=0x7f040001;  
  19.          public static final int hello=0x7f040000;  
  20.     }  

通過上面代碼,我們可以看到這些常量與res文件夾中的文件名字一樣,說明了R.java文件存儲了該項目中的所有資源索引。R.java文件的存在,方便資源的使用,當我們在項目中加入新資源,只需刷新項目,R.java文件就會自動生成所有資源的索引。分析上面的xml文件:

2 AndroidManfest.xml文件包含了項目中所有使用的Activity、Service、Receiver,代碼如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  3.      package="com.hanfeng.demo" 
  4.       android:versionCode="1" 
  5.       android:versionName="1.0"> 
  6.     <uses-sdk android:minSdkVersion="8" />   
  7.     <application android:icon="@drawable/icon" android:label="@string/app_name"> 
  8.         <activity android:name=".AndroidTest" 
  9.                  android:label="@string/app_name"> 
  10.             <intent-filter> 
  11.                 <action android:name="android.intent.action.MAIN" /> 
  12.                <category android:name="android.intent.category.LAUNCHER" /> 
  13.             </intent-filter> 
  14.         </activity>   
  15.     </application> 
  16.  </manifest> 
 分析上面的xml文件:

【xmlns:android】:包含命名空間的聲明。xmlns:android="http://schemas.android.com/apk/res/android",使得Android中各種標準屬性能夠在文件中使用,提供大部分元素的數據。

【package】:聲明應用程序包。

【application】:包含package中application級別組件聲明的根節(jié)點。此元素耶可包含application的一些全局和默認的屬性,如標簽、icon、主題、必要權限等。一個manifest能夠包含零個或一個此元素,不能大于一個。

【android:icon】:應用程序圖標。

【android:lebel】:應用程序名字。

【Activity】:用戶交互工具。

【android:name】:應用程序默認啟動的Activity。

【intent-filter】:聲明了指定一組組件支持的Intent值,從而形成IntentFilter。

【action】:組件支持的Intent action 。

【category】:組件支持的Intent Category。指定應用程序默認啟動的Activity。

【uses-sdk】: 應用程序所使用的sdk版本。

#p#

3 String.xml 資源文件中的常量定義。代碼如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  <resources> 
  3.    <string name="hello">Hello World, AndroidTest!</string> 
  4.    <string name="app_name">AndroidDemo</string> 
  5. </resources> 

從上面的代碼可以看出,文件中定義的字符串資源常量"hello"、"app_name"指向了R.java文件中的字符串資源。

下面看如何使用定義的這些資源。我們可以通過Context的getResource實例化一個Resource對象,然后通過Resource的getSting方法獲取指定索引的字符串。代碼如下:

  1.  Resource r = this.getContext().getResource();  
  2.  String appname = ((String) r.getString(R.string.app_name));  
  3. String hello = ((String) r.getString(R.string.hello));  

4 main.xml布局文件,代碼如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello" 
  11.     /> 
  12. </LinearLayout> 

布局參數解析:

【<linearLayout>】:線性版面配置,在這個標簽中,所有元件都是按由上到下的排列排成。

【android:orientation】:表示這個介質的版面配置方式是從上到下的垂直地排列其內部視圖。

【android:layout_width】:定義當前視圖在屏幕上所占的寬度,fill_parent即填充整個屏幕。

【android:layout_height】:定義當前視圖在屏幕上所占的高度。

【wrap_weight】:隨著文字欄位的不同而改變這個視圖的高度或寬度。

5 AndroidTest.java 項目的豬程序文件。代碼如下:

  1. package com.hanfeng.demo;  
  2.  
  3.  import android.app.Activity;  
  4.  import android.os.Bundle;  
  5.  
  6.  public class AndroidTest extends Activity {  
  7.     /** Called when the activity is first created. */  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.main);  
  12.     }  

主程序AndroidTest繼承自Activity類,重寫了void onCreate(Bundle saveInstanceState)方法。在onCreate方法中通過setContentView(R.layout.main)設置Activity要顯示的布局文件。

【編輯推薦】

Android開發(fā)之旅:Android架構

Android應用程序開發(fā)環(huán)境的搭建

在Android應用程序中使用Internet數據

Android核心分析之一->分析方法論探討之設計意圖

責任編輯:zhaolei 來源: 網絡轉載
相關推薦

2018-04-27 10:59:30

Linux目錄結構lib

2010-04-15 11:47:37

微軟活動目錄邏輯結構

2013-01-16 14:19:03

Android工程目錄結構Android開發(fā)

2013-01-17 15:26:21

Android工程目錄結構Android開發(fā)

2011-09-14 16:33:04

2013-05-23 15:18:13

Android開發(fā)移動開發(fā)程序目錄結構

2013-10-30 22:50:30

Clouda結構

2010-03-09 14:04:28

2011-01-10 10:30:05

linux目錄結構

2014-04-28 16:13:11

Unix目錄結構

2012-02-08 09:48:25

開源項目

2013-03-22 17:12:34

Android工程代碼結構

2010-11-03 16:50:23

DB2目錄結構

2010-05-26 19:05:06

SVN庫

2010-05-26 19:36:34

SVN目錄結構

2010-05-27 10:53:54

SVN目錄結構

2010-11-02 09:56:14

DB2目錄結構

2014-03-06 10:50:59

iOS開發(fā)

2022-07-18 05:59:18

Linux目錄結構操作系統(tǒng)

2010-09-27 13:30:03

IP協(xié)議首部
點贊
收藏

51CTO技術棧公眾號