繁体   English   中英

如何将OnClickListener用于RelativeLayout ViewGroup?

[英]How I can use the OnClickListener for RelativeLayout ViewGroup?

嘿,我的Android应用程序中有网格布局。 我想在我的RelativeLayout上使用OnClickListener。 但这不起作用。

这是我的布局:

<RelativeLayout
    android:id="@+id/menu_button_logout"
    android:background="@color/tran_colorYellow"
    android:layout_height="fill_parent"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:gravity="center"
    android:clickable="true">

    <ImageView
        android:id="@+id/categoria_image_2"
        android:src="@drawable/logo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"/>

    <TextView
        android:text="Abmelden"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:gravity="center"
        android:layout_below="@+id/categoria_image_2"
        android:textAllCaps="true"
        android:textStyle="bold"
        android:textColor="@color/white"/>

</RelativeLayout>

这是我的代码:

relativeLayoutLogout = (RelativeLayout)findViewById(R.id.menu_button_logout);
relativeLayoutLogout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mAuth.getInstance().signOut();
    }
});

尝试将此行添加到子视图android:clickable="false"

<RelativeLayout
    android:id="@+id/menu_button_logout"
    android:background="@color/tran_colorYellow"
    android:layout_height="fill_parent"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:gravity="center"
    android:clickable="true">

<ImageView
    android:id="@+id/categoria_image_2"
    android:src="@drawable/logo"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:clickable="false"/>

<TextView
    android:text="Abmelden"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:gravity="center"
    android:layout_below="@+id/categoria_image_2"
    android:textAllCaps="true"
    android:textStyle="bold"
    android:textColor="@color/white"
    android:clickable="false"/>

</RelativeLayout>

如果要在根视图上获取click事件,请尝试使用OnInterceptTouchEvent捕获事件。 此处的更多信息https://developer.android.com/guide/topics/ui/ui-events.html#EventHandlers

暂无
暂无

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

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