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

自訂組合 View 的陷阱(II):多餘的 LinearLayout 與 merge 標籤

編輯:Android 開發實例

上一篇我們描述了組合 View 使用不當時出現的怪異現象,本文來拆解它的成因與解法。

問題根源:多了一層 Layout

多數開發者會直覺認為,寫一個 IconTextView 這種自訂 View,就等於自訂了一個「内含一個 ImageView 加兩個 TextView」的組合控制項。但實際上,View 的階層結構中多出了一層 LinearLayout。

仔細看就會發現:IconTextView 本身繼承自 LinearLayout,而 icon_text_view.xml 的根元素也是 LinearLayout。於是真正的結構變成「兩層 Layout 裡面包住三個單一 View」。用 Android SDK 的 hierarchyviewer.bat 工具就能清楚看到這個多餘的階層。

而在 icon_text_view.xml 中,我們把 android:focusable 與 android:clickable 設在了內層那個 LinearLayout 上。結果外層的自訂 LinearLayout(也就是 IconTextView 本身)反而拿不到焦點與點擊事件,掛在 IconTextView 上的 OnClickListener 自然也不會被觸發。

想讓這個階層視覺化,可以幫 IconTextView 加上一個明顯的背景:

IconTextView itv = new IconTextView(this);
itv.setBackgroundResource(android.R.drawable.btn_default);

重新執行後就能看出 Layout 的層級關係:有兩個 Layout 疊在一起。點擊內層 Layout 時它會有 Press 視覺回饋,但因為事件沒有註冊在它身上,所以不會有任何行為;若改成讓外層 Layout 可以響應點擊,內層就再也拿不到 Press 狀態了——這正是「按下效果」與「點擊事件」魚與熊掌不可兼得的癥結。

解法:用 merge 取代 LinearLayout

既然問題來自多餘的階層,最直接的解法就是把 icon_text_view.xml 中的 Layout 拿掉,改用 Android 提供的 <merge> 元素,同時在 IconTextView 類別中(或把這個組合 View 加入其他 ViewGroup 時)明確指定它可以取得焦點。

版面配置改為 merge_icon_text_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="https://schemas.android.com/apk/res/android" >
    <ImageView android:src="@drawable/feedicon"
               android:layout_width="wrap_content"
               android:layout_height="fill_parent" />

    <LinearLayout android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content">
        <TextView android:text="merge Line one"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>
        <TextView android:text="merge Line two"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>
    </LinearLayout>
</merge>

自訂 View 類別別忘了設定 focusable:

public class IconTextView extends LinearLayout {

    ImageView mImageView;
    TextView  mTextOne;
    TextView  mTextTwo;

    public IconTextView(Context context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.merge_icon_text_view, this, true);
        setFocusable(true);   // 關鍵:讓外層可以取得焦點
        // 這裡接著找出 mImageView、mTextOne、mTextTwo
    }

    public void setData(Drawable icon, String line1, String line2) {
        mImageView.setImageDrawable(icon);
        mTextOne.setText(line1);
        mTextTwo.setText(line2);
    }
}

總結

撰寫自訂組合 View 時,務必留意 ViewGroup 的階層是否有多餘的包裝;若發現非必要的 ViewGroup,可以用 <merge> 元素把它消除。這不只解決事件與焦點的衝突,也能減少 measure / layout 的開銷。

補充:另一個等效做法是在版面配置中直接寫入自訂 View 的完整類名,例如把上面的 <merge> 換成 <org.goodev.IconTextView>,讓 Android 在 inflate 時直接建立該控制項。

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