简体   繁体   中英

How I can use the OnClickListener for RelativeLayout ViewGroup?

hey i have a grid layout in my android application. I want to use a OnClickListener on my RelativeLayout. But it don't work.

Here my Layout:

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

Here my Code:

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

Try adding this line to the child views 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>

Try to catch event using OnInterceptTouchEvent if you want to get click event on root view. More information here https://developer.android.com/guide/topics/ui/ui-events.html#EventHandlers

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