简体   繁体   English

LinearLayout如何将onPressed事件传递给其子元素?

[英]How can LinearLayout transfer onPressed event to its child elements?

I have a LinearLayout and its child - ImageView. 我有一个LinearLayout及其子 - ImageView。 My activity's onCreate does nothing but for setting content view. 我的活动onCreate除了设置内容视图外什么都不做。 Content view's xml source looks like this: 内容视图的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>

Where @drawable/selector is: @ drawable / selector的位置是:

    <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. 案例1:我按ImageView。

Case 2: I press LinearLayout outside of ImageView. 案例2:我在ImageView之外按LinearLayout。

In the 1st case ImageView changes its src image (from foo2 to foo1), in the 2nd case it doesn't change anything. 在第一种情况下,ImageView更改其src图像(从foo2到foo1),在第二种情况下,它不会更改任何内容。 How can I make ImageView change its src image in the 2nd case? 如何在第二种情况下让ImageView更改其src图像?

Try setting 尝试设置

android:duplicateParentState="true"

on your children xml elements. 在你的孩子xml元素上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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