Android LinearLayout如何将onPressed事件传输到其子元素?

Android LinearLayout如何将onPressed事件传输到其子元素?,android,events,selector,Android,Events,Selector,我有一个线性布局和它的子图像视图。我的活动的onCreate只用于设置内容视图。内容视图的xml源如下所示: <?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="

我有一个线性布局和它的子图像视图。我的活动的onCreate只用于设置内容视图。内容视图的xml源如下所示:

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

其中@drawable/选择器为:

    <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:我在ImageView外按LinearLayout

在第一种情况下,ImageView更改其src图像(从foo2更改为foo1),在第二种情况下,它不会更改任何内容。如何使ImageView在第二种情况下更改其src图像?

尝试设置

android:duplicateParentState="true"

在您的子xml元素上。

现在它在案例2中有效,但在案例1中不起作用:ImageView接收onPressed事件,但线性布局不起作用。如果我想让image view在第一种情况下也更改图像,我该如何处理此问题?请尝试在ImageView上将“clickable”设置为false,让LinearLayout处理所有单击事件,并在ImageView上保持“duplicateParentState”以使两者保持同步。我添加了android:clickable=“false”和android:duplicateParentState=“true”在儿童控制中。现在工作很好。thanx:)