編輯:Android 開發實例
layout_width(寬):fill_parent 表示與父元素同寬,wrap_content 表示隨內容調整,也可直接指定 px。
layout_height(高):同上。
background:設定背景圖。
padding:內距,可分別用 paddingBottom、paddingLeft、paddingRight、paddingTop 指定不同 px。
id:@+id/id1 代表新增一個名為 id1 的 id;@id/id1 則是引用既有的 id1。
layout_weight:可理解為「佔用剩餘空間的權重」。預設 0,數值越大分到的空間越多。
layout_gravity:元件在容器中的對齊方式。
gravity:文字等內容在元件內部的對齊方式,例如讓 Button 的文字置中。
以上多數屬性都有對應的方法可在執行期動態改變,細節可查 SDK 文件。
orientation 決定子元素的排列方向:vertical 為垂直排列,horizontal 為水平排列;程式碼中用 setOrientation() 動態改變,值為 HORIZONTAL 或 VERTICAL。
這裡有個關鍵特性:LinearLayout 的寬/高是按元件順序逐一佔用的。因此當某個元件設成 fill_parent 而沒有搭配 layout_weight 時,它會吃掉所有剩餘空間,排在它後面的元件就完全不見了。例如下面範例的 EditText 若少了 android:layout_weight="1",其下方的元件就會被壓得不見。
baselineAligned 預設為 true,代表同一方向的元件會以第一個元件為基準對齊。這也是為什麼下圖中 text1、button1、text2 的文字會落在同一水平線上;不需要這個效果時設為 false 即可。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="hello"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/edittext" />
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="text1"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button android:text="Button01"
android:id="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:text="text2"
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="buttom" />
</LinearLayout>
實務上的觀念是:weight 分配的是「扣掉 wrap_content 內容之後剩下的空間」,而不是全部空間。同一層級裡若有多個元件設了 weight,就依比例瓜分。另外,若要讓子層的 weight 正常運作,父層(或父父層)對應方向的 layout_width/layout_height 必須是 fill_parent,否則會被壓縮到剛好容納內容的大小。
這個版面直觀易懂,但元件之間容易形成依賴關係,「牽一髮而動全身」。因此適合用來描述結構相對固定的畫面。
<RelativeLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/relativelayout">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon" />
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello"
android:layout_toRightOf="@id/image" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="button1"
android:layout_toRightOf="@id/image"
android:layout_below="@id/text1" />
</RelativeLayout>
用程式碼動態加入元件:
public class RelativeDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.relative);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, // width
ViewGroup.LayoutParams.WRAP_CONTENT // height
);
// 相當於 layout_below="@id/button1"
lp.addRule(RelativeLayout.BELOW, R.id.button1);
// 相當於 layout_alignLeft="@id/image"
lp.addRule(RelativeLayout.ALIGN_LEFT, R.id.image);
((RelativeLayout) findViewById(R.id.relativelayout))
.addView(new EditText(this), lp);
}
}
撰寫順序上,先放好作為參照的元件(例如 ImageView),再依序加入其他元件並定義位置規則。規則宣告的本身不分先後,因為 RelativeLayout 會在 measure 階段統一解析依賴關係。另外提醒,ADT 外掛提供的預覽圖有時不夠即時,最終還是要上模擬器或真機確認。
屬性雖多,基本上可分成兩類:
1. 需要指定參照物的:layout_alignBottom、layout_toLeftOf、layout_above、layout_alignBaseline 系列,例如 layout_above="@id/text1"。
2. 以 parent 為參照物、值為 true/false 的:layout_centerVertical、layout_alignParentLeft 系列,例如 layout_alignParentLeft="true"。
其中 layout_alignWithParentIfMissing 值得特別注意:設為 true 時,若指定的參照物找不到,系統會改用 parent 作為新的參照物,這在元件可能被動態隱藏或移除時是相當實用的保險。
位置規則由這個靜態類別承載,前述 XML 屬性都出自於此。它的 addRule(int verb, int anchor) 中,verb 是動作常數(對應部分 XML 屬性),anchor 則可能是參照元件的 id、RelativeLayout.TRUE,或不需要 anchor 時傳 -1。可以這樣記憶:
xml 屬性 (verb) = "value" (anchor)
它的建構子 RelativeLayout.LayoutParams(int w, int h) 則用來指定子 View 的寬高。
LinearLayout 適合由上而下、由左而右的線性堆疊,層數淺、好維護;RelativeLayout 適合以「某元件相對於另一元件」描述的位置關係,能避免多層嵌套。實務上兩者常混用:外層 LinearLayout 負責區塊排列,內層 RelativeLayout 處理單一區塊內的定位。原則是寧可用少一點層級把畫面撐起來,再搭配前述的 merge/include 做優化。
本文整理自「學習Android」部落格:https://android.blog.51cto.com/268543/298345
Android 登入畫面實作:帳密驗證與失敗鎖定
登入畫面幾乎是每個需要帳號的 App 都會遇到的介面。使用者在此輸入憑證,App 驗證通過後才放行到下一個頁面。本章要拆解一個登入畫面的三個組成部分:輸入欄位、驗證邏輯,
Android 自訂輸入法 IME 開發教程
輸入法編輯器,又稱 IME,是 Android 中讓使用者輸入文字的元件。開發者可自訂軟鍵盤、語音輸入或其他輸入方式,並讓使用者在系統設定中選擇。 基本架構 自訂 IM
Android 設置全屏的兩種常用做法
幾乎每個 Android 項目都會遇到全屏需求,特別是遊戲和媒體類應用。實現方式主要有兩種,選其一即可。 方法一:在配置文件裡聲明主題 直接在 AndroidManif
Android 開發實作:日曆專案(三)建立樣式與資源
接續日曆專案的進度,這一階段要把視覺資源整理乾淨:顏色、尺寸、Color State List、drawable 與 style 全部抽成獨立檔案。日曆的表格線以 Cel