【正文】
會加入完整的 GUI 拖拉設(shè)計工具。但在 ADT 加入完整的 GUI 拖拉設(shè)計工具之前,已經(jīng)有人寫出來了對應(yīng) Android 的 GUI 拖拉設(shè)計工具,可供使用。DroidDraw Android GUI 拖拉設(shè)計工具XmlR存取識別符號在上一章談了 XML描述檔中介面元件的各種「android:」開頭的屬性。要使用一個介面元件,第一件事就是定義出介面描述檔。大部分的介面元件(如 LinearLayout、TextView)不需要在程式中作後續(xù)處理,因此可以直接描述(例如我們前面定義過的 TextView 文字顯示元件「身高(cm)」)。但對於那些將在程式中被參考(reference)到的介面元件(如按鈕 Button、文字輸入欄位 EditText),我們需要先行在 XML描述檔中,定義該介面元件的「android:id」識別符號屬性。這麼一來在程式碼中我們要操作這個介面元件(取出欄位中輸入的資料、取得按下按鈕事件...)時,就能透過「android:id」識別符號來調(diào)用這個介面元件。EditText android:id=@+id/height /前面章節(jié)提過,寫作時最好將 XML 描述檔屬性分行列出,以易於閱讀(增加可讀性)。而我們的範(fàn)例卻將 android:id 屬性直接擺在 EditText 標(biāo)籤後。其實(shí)這麼做同樣是基於易於閱讀的考量。當(dāng)然你也可以將「android:id」屬性分行列出,或是將「android:id」屬性放在屬性列表的中間或最後頭,這些作法都是允許的,本書中一律採用將 android:id 屬性直接擺在介面元件標(biāo)籤後的寫法。android:id 屬性的內(nèi)容長得比較特別:@+id/height「height」是這個介面元件的 android:id。以後的程式中會使用「」來取得這個介面元件。「@+id」 的意思是我們可以通過這個識別符號來控制所對應(yīng)的介面元件,「R」類別會自動配置一個位址給這個介面元件。 「R」類別的內(nèi)容則可以透過查看 得知。XML 描述檔與 檔在 Android 系統(tǒng)中,我們使用 XML 來定義 UI。但是有些稍微有經(jīng)驗的開發(fā)者可能會有疑問:「用 XML 來描述介面固然方便,但是對於手機(jī)程式來說,直接用 XML 檔案是不是太占空間了?」。沒錯,如果 Android 是直接使用 XML 來儲存介面描述到手機(jī)上的話,一定會佔(zhàn)用比起現(xiàn)在大的多的檔案空間。解決的方法是Android 並不直接使用 XML 檔案,而是透過 Android 開發(fā)工具,自動將 XML 描述檔轉(zhuǎn)換成資源檔案。一旦應(yīng)用程式要操作某個介面元件,或是使用任何種類的資源(字串、圖片、圖示、音效...),都使用索引來查詢。當(dāng)你建立一個 BMI 新專案,打開位於 「gen//demo/android/bmi」 目錄下的 「」檔,你可以看到如下的程式碼: /* AUTOGENERATED 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 。 public final class R { public static final class attr { } public static final class drawable { public static final int icon=0x7f020000。 } public static final class layout { public static final int main=0x7f030000。 } public static final class string { public static final int app_name=0x7f040000。 } }在照著前一章新增了 XML 描述後,再次打開打開 「gen//demo/android/bmi」 目錄下的 「」 檔 ,你可以看到如下的程式碼: /* AUTOGENERATED 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 。 public final class R { public static final class attr { } public static final class drawable { public static final int icon=0x7f020000。 } public static final class id { public static final int height=0x7f050000。 public static final int result=0x7f050003。 public static final int submit=0x7f050002。 public static final int suggest=0x7f050004。 public static final int weight=0x7f050001。 } public static final class layout { public static final int main=0x7f030000。 } public static final class string { public static final int app_name=0x7f040000。 } }我們看到在 檔案中,分別有 attr (屬性)、drawable (圖片、圖示)、id (識別符號)、layout (介面描述)、string (文字) 這幾種資源型態(tài),就 XML 描述檔中的 id 來說,開發(fā)工具會根據(jù) XML 描述檔中指定的 id,生成對應(yīng)的資源,並自動指定一個位址。Google 官方文件是這麼解釋「」檔案的作用的:A project39。s file is an index into all the resources defined in the file. You use this class in your source code as a sort of shorthand way to refer to resources you39。ve included in your project. This is particularly powerful with the codepletion features of IDEs like Eclipse because it lets you quickly and interactively locate the specific reference you39。re looking for.The important thing to notice for now is the inner class named layout, and its member field main. The Eclipse plugin noticed that you added a new XML layout file and then regenerated this file. As you add other resources to your projects you39。ll see change to keep up.有了「」做中介,在 XML 描述檔中,我們可以透過@[類型]/[識別符號]這樣的語法來為某個介面元件提供識別符號,以供程式控制。例如,我們可以用 「@+id/height」來為對應(yīng)供輸入身高數(shù)字的 EditText 元件提供識別符號。將字串抽離 XML當(dāng)我們在 res 資料夾中新增各種一個 XML 檔案,或是一張圖片時,開發(fā)工具會從 res 資料夾中蒐集,並將各種資源彙整成一個索引,自動產(chǎn)生出 檔。透過這個特性,我們可以進(jìn)一步加工我們的 XML 描述檔,讓介面更易於維護(hù)。開啟 res/values/原始的內(nèi)容為?xml version= encoding=utf8?resources string name=app_nameBMI/string/resources裡面只定義了一個字串「app_name」,用來表示應(yīng)用程式名稱(在之後講解.xml 檔案時將會用到)。 我們看到表示字串的格式為string name=識別代號文字?jǐn)⑹?string我們將上一章中的敘述抽取出來,整理進(jìn) 檔案。完整的 檔案如下:?xml version= encoding=utf8?resources string name=app_nameBMI/string string name=bmi_height身高 (cm)/string string name=bmi_weight體重 (kg)/string string name=bmi_btn計算 BMI 值/string string name=bmi_result你的 BMI 值是 /string/resources在 檔案中,我們在原本的 app_name 字串外,自行定義了另外幾個字串。如果我們再次開啟 「」檔,我們會發(fā)現(xiàn)檔案中的 string 類別中也自動索引了上面定義好的字串: public static final class string { public static final int app_name=0x7f040000。 public static final int bmi_btn=0x7f040003。 public static final int bmi_result=0x7f040004。 public static final int bmi_height=0x7f040001。 public static final int bmi_weight=0x7f040002。 }接著,我們把這些字串應(yīng)用到之前定義好的 XML 描述檔中。透過使用@string/[識別符號]這樣存取 string 類型的格式,來取代 檔案中原本寫死的文字?jǐn)⑹觥M暾某淌酱a如下:?xml version= encoding=utf8?LinearLayout xmlns:android= android:orientation=vertical android:layout_width=fill_parent android:layout_height=fill_parent TextView android:layout_width=fill_parent android:layout_height=wrap_content android:text=@string/bmi_height / EditText android:id=@+id/height android:layout_width=fill_parent android:layout_height=wrap_content