使用ImageButton捕获LinearLayout onClick事件

Ric*_*ard 6 android imagebutton android-linearlayout onclicklistener

我有一个ImageButton和一个包含在LinearLayout中的TextView,如下所示:

    <LinearLayout android:orientation="vertical"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_weight="20" android:gravity="center"
        android:clickable="true" android:id="@+id/action_showhide">
        <ImageButton android:id="@+id/test"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:src="@drawable/ic_toggle_hide_states" android:background="@null"></ImageButton>
        <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="@string/txtHide"
            android:textColor="@drawable/orange" android:textStyle="bold"></TextView>
    </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

ImageButton由正常,聚焦和按下状态的自定义drawable支持.我想允许用户单击LinearLayout中的任何位置来触发OnClick事件.下面的代码显示了OnClickListener的设置:

    final LinearLayout actionHide = (LinearLayout) findViewById(R.id.action_showhide);
    actionHide.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(AppAdvocate.TAG, "Event caught");
        }
    });
Run Code Online (Sandbox Code Playgroud)

当用户点击任意位置上的TextView但是当用户点击ImageButton的事件不能上升到的LinearLayout代码工作.我没有为按钮定义onClickListener.我希望我的ImageButton的drawable可以更改,所以我不想将它设置为clickable = false.有没有办法搞砸事件?

Che*_*mon 0

如果您确实不想让按钮不可点击,您可以向按钮添加一个侦听器并执行与 LinearLayout onClick 相同的操作。对于用户来说,它就像一个大按钮。