Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

子 View 的 layout_margin 失效?多包一層 Layout 就能解決

編輯: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" 完全沒有作用,背景圖直接貼死在邊界上。

解法:在外層再包一個 LinearLayout

<?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。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved