標籤:
我們先學習如何使用xml代碼來定義布局。
在當前的activity_quic.xml檔案中,定義了預設的activity布局。應用的預設布局經常改變,但是xml布局檔案卻總是與代碼清單1-1檔案相似。
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".QuizActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /></RelativeLayout>
代碼清單1-1 代碼清單1-1:預設的activity布局(activity_quiz.xml)
首先,我們注意到activity_quiz.xml檔案不再包含版本聲明與檔案編碼的如下代碼:
<?xml version = "1.0" encoding = "utf-8" ?>
ADT21開發版本以後,android布局檔案已不再需要該行代碼。不過,很多情況下,可能還會看到它。
應用activity的布局預設定義了兩個組件(widget):RelativeLayout 和 Textview.
組件 是組成使用者介面的構造模組。按鈕、文本輸入控制項和選擇框等都是組件。
Android SDK內建了多種組件,通過配置各種組件可獲得所需的使用者介面及行為。每個組件是View類或其子類(如TextView或Button)的一個具體執行個體。
QuizActivity的使用者介面需要下列五個組件:
1. 一個垂直LinerLayout組件;
2. 一個TextView組件
3. 一個水平LinerLayout組件;
4. 兩個Button組件。
布置並顯示在螢幕上的組件。
下面我們在activity_quiz.xml檔案中定義這些組件。
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:gravity = "center" android:orientation = "vertical" > <TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:padding = "24dp" android:text = "@string/question_text" /> <LinearLayout android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:orientation = "horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/true_button" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/false_button" /> </LinearLayout> </LinearLayout>
代碼清單1-2:在XML檔案(activity_quiz.xml)中定義組件
需要特別注意的是:開發工具無法校正布局XML內容,請避免輸入或拼字錯誤。
根據所使用的工具版本不同,用三行以android:text開頭的代碼可能會產生錯誤資訊。暫時忽略它們,稍後處理。
可以看出,元素名稱就是組件的類型。
各元素均有一組xml屬性。屬性可以看作是如何配置組件的指令。
視圖階層
組件包含在視圖對象的層級結構,即視圖層級結構(view hieratchy)中。圖1-10展示了代碼清單1-2所示的xml布局對應的視圖層級結構。
從上可以看出,根項目是一個LinearLayout組件。作為根項目,LinearLayout組件必須指定Android xml資源檔的命名空間屬性為:http://schemas.android.com/apk/res/android。
LinearLayout組件繼承自View子類的ViewGroup組件。ViewGroup組件是一個包含並配置其他組件的特殊組件。如需以一列或一排的樣式布置組件,使用LinearLayout組件就可以了。其他ViewGroup子類還包括FrameLayout、TableLayout和RelativeLayout。
若某個組件包含在一個ViewGroup中,該組件與ViewGroup即構成父子關係。根LinearLayout有兩個子組件:TextView和LinearLayout。作為子組件的LinearLayout本身還有兩個Button子組件。
組件屬性
下面我們一起來看看配置組件的一些常用屬性。
1. android:layout_width和android:layout_height屬性
幾乎每類組件都需要android:layout_width和android:layout_height屬性。它們通常被設定為以下兩種屬性值之一。
? match_parent:視圖與其父視圖大小相同。
? wrap_content:視圖將根據其內容自動調整大小。
(以前還有一個fill_parent屬性值,等同於match_parent,目前已廢棄不用。
TextView組件比其包含的文字內容地區稍大一些,這主要是android:padding="24dp"屬性的作用。該屬性告訴組件在決定大小時,除內容本身外,還需要增加額外指定量的空間。這樣螢幕上顯示的問題與按鈕之間便會留有一定的空間。(dp即density-independent pixel,指與密度無關像素,第八章將介紹有關它的概念。)
2. android:orientation屬性
android:orientation屬性是兩個LinearLayout組件都具有的屬性,決定了組件是水平放置還是豎直放置的。在上面xml中,根LinearLayout是垂直的,子LinearLayout是水平的。
LinearLayout子組件的定義順序決定著其在螢幕上顯示的順序。在豎直的LinearLayout中,第一個定義的子組件出現在螢幕的最上端。而在水平的LinearLayout中,第一個定義的子組件出現在螢幕的最左端。(如果裝置語言是從右至左顯示,則第一個定義的子組件則出現在螢幕的最右端。)
3. android:text屬性
TextView與Button組件具有android:text屬性。該屬性指定組件顯示的文字內容。
請注意,android:text屬性值不是字串字面值,而是對字串資源(string resources)的引用。
字串資源套件含在一個獨立的名為strings的XML檔案中,雖然可以寫入程式碼設定組件的文字屬性, 如android:text="True", 但這通常不是個好方法。將文字內容放置在獨立的字串資源XML檔案中,然後引用它們才是好方法。
需要在activity_quiz.xml檔案中引用的字串資源目前還不存在。現在我們來添加這些資源。
(原創)《Android編程權威指南》學習筆記01-- Android應用初體驗--004