简体   繁体   中英

How can LinearLayout transfer onPressed event to its child elements?

I have a LinearLayout and its child - ImageView. My activity's onCreate does nothing but for setting content view. Content view's xml source looks like this:

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

Where @drawable/selector is:

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

Case 1: I press ImageView.

Case 2: I press LinearLayout outside of ImageView.

In the 1st case ImageView changes its src image (from foo2 to foo1), in the 2nd case it doesn't change anything. How can I make ImageView change its src image in the 2nd case?

Try setting

android:duplicateParentState="true"

on your children xml elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM