android - LinearLayout 如何将 onPressed 事件传递给它的子元素?

标签 android events selector

我有一个 LinearLayout 及其子项 - ImageView。我的 Activity onCreate 除了设置内容 View 外什么都不做。内容 View 的 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 在哪里:

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

在您的 child xml 元素上。

关于android - LinearLayout 如何将 onPressed 事件传递给它的子元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9791474/

相关文章:

Android:像短信对话一样的ListView风格

jquery - 根据 ID 按顺序显示 LI

javascript - 使用 jQuery 防止 onclick Action

用于 sIFR 菜单的 CSS 选择器

android - 单击网络应用程序中的按钮会显示按钮后面的输入,然后无意中关注输入

android - 廉价的支持 Wi-Fi 的 Android 或其他可编程设备

java - 有目的地将 Activity 放在首位

ruby - Ruby 中的事件处理

Swift Enum to Cocoa Touch UIButton 选择器字符串

javascript - eq 不正确,第二个元素