編輯:Android 開發實例
這是一個還滿容易撞到的狀況:明明是動態加入的子 View,也明明寫了 android:layout_margin="1dip",那 1dip 的間距卻怎麼都不肯出現。
原本的做法是把一段 LinearLayout 直接當成子 View 加入:
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical"
android:id="@+id/linearLayout0"
android:layout_margin="1dip"
android:background="@drawable/cd_linearlayout_background"
xmlns:android="https://schemas.android.com/apk/res/android">
<ImageView android:layout_width="106dip"
android:layout_height="110dip"
android:layout_weight="1.0"
android:id="@+id/imageView0"
android:src="@drawable/a1" />
<TextView android:layout_width="match_parent"
android:layout_height="25dip"
android:id="@+id/textView0"
android:text="0"
android:textColor="@color/black"
android:textSize="10dip"
android:layout_marginLeft="5dip" />
</LinearLayout>
結果那個 layout_margin="1dip" 完全沒有作用,背景圖直接貼死在邊界上。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical"
xmlns:android="https://schemas.android.com/apk/res/android">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical"
android:id="@+id/linearLayout0"
android:layout_margin="1dip"
android:background="@drawable/cd_linearlayout_background">
<ImageView android:layout_width="106dip"
android:layout_height="110dip"
android:layout_weight="1.0"
android:id="@+id/imageView0"
android:src="@drawable/a1" />
<TextView android:layout_width="match_parent"
android:layout_height="25dip"
android:id="@+id/textView0"
android:text="0"
android:textColor="@color/black"
android:textSize="10dip"
android:layout_marginLeft="5dip" />
</LinearLayout>
</LinearLayout>
多包一層之後,android:layout_margin="1dip" 的效果就正常出現了。
關鍵在於 layout_margin 是由父容器解讀的屬性,它屬於 LayoutParams 的一部分,而不是 View 自身的繪製屬性。也就是說,一個 View 能不能有 margin,取決於「它的父容器讀不讀這個值」,以及「加入時有沒有帶進對應的 LayoutParams」。
當你以程式碼把一個已經在 XML 中 inflate 出來的 View 直接 addView 到某個容器,若沒有明確指定 LayoutParams,系統可能採用預設值或沿用原本的參數,margin 就容易被忽略。而多包一層之後,這個 View 是在 XML 階層中正常被 inflate 的,父子關係明確,margin 自然生效。
如果你不想多一層容器(畢竟多一層就多一次 measure 與 layout 的開銷),可以改用 android:padding 搭配背景圖自身的九宮格留白,或者直接在程式碼中明確建立 LayoutParams:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
lp.setMargins(1, 1, 1, 1);
container.addView(childView, lp);
這種寫法把 margin 直接寫進 LayoutParams,不依賴父容器的 XML 設定,是更可控的方式。三種做法何者較好取決於情境:快速修補用多包一層,正式專案建議明確指定 LayoutParams。
Android 實作 Market 風格的 Loading 效果:自訂版面取代 ProgressDialog
在 Android 裡做載入提示,直覺做法是 ProgressDialog。ApiDemos 中的 ProgressBar3.java 就示範了兩種典型用法:但仔細看 A
動手做 Android 本地化:以英文與義大利文為例
App 的下一個使用者,可能住在米蘭、大阪或首爾。要讓他們覺得這個 App 是為自己做的,最基本的一步就是讓介面說他們的語言。Android 的資源系統把這件事變得相當直
Android JSON 解析入門:用 JSONObject 抓出你要的那幾個欄位
JSON 是 JavaScript Object Notation 的縮寫,做為一種與語言無關的資料交換格式,這些年在行動端幾乎全面取代了 XML:它更輕、更好讀,解析也
Android 登入頁面 from scratch:輸入欄位、錯誤提示到次數鎖定
登入頁面是所有需要帳號的 App 都躲不掉的一關。它看起來只是兩個輸入框加一顆按鈕,實際上要顧的事情不少:密碼欄必須遮蔽輸入、按鈕要綁定事件、錯誤時要給明確回饋,還得有一