[英]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.