2012-03-20 24 views
2

我有一個LinearLayout和它的孩子 - ImageView。我的活動的onCreate除了設置內容視圖外什麼都沒做。內容視圖的XML源代碼看起來是這樣的:LinearLayout如何將onPressed事件轉移到其子元素?

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:clickable="true" 
     android:gravity="center"  
     android:background="@android:color/white"  
     android:layout_height="wrap_content" > 

     <ImageView 
      android:layout_width="140dp" 
      android:layout_height="80dp" 
      android:background="@android:color/darker_gray" 
      android:clickable="true" 
      android:src="@drawable/selector" /> 
    </LinearLayout> 

</LinearLayout> 

凡@繪製/選擇是:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/foo1" android:state_pressed="true"/> 
    <item android:drawable="@drawable/foo2"/> 

</selector> 

案例1:我按ImageView的。

情況2:我在LinearView之外按LinearLayout。

在第一種情況下,ImageView將其src圖像(從foo2改爲foo1),在第二種情況下它不會改變任何東西。如何讓ImageView在第二種情況下更改其src圖像?

回答

9

嘗試設置

android:duplicateParentState="true" 
上你的孩子XML元素

+0

現在它在情況2下工作,但在情況1下不起作用:ImageView收到onPressed事件,但線性佈局沒有。如果我希望圖像視圖在第一種情況下更改圖像,我該如何處理這個問題? – 2012-03-21 08:32:41

+3

嘗試在ImageView上將'clickable'設置爲false,讓LinearLayout處理所有單擊事件,並在ImageView上保留'duplicateParentState'以使兩者保持同步。 – danh32 2012-03-21 13:44:24

+0

現在完美運行,謝謝! – 2012-03-22 09:28:15

相關問題