android-无法在相对布局上执行setonclicklistener

krcsximq  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(476)

我试图在相对布局上执行setonclicklistener,但无法执行意味着没有响应。我尝试了一个log语句来检查它是否被单击,但是没有显示任何内容 Logcat .
下面是xml代码

  1. <com.google.android.material.appbar.AppBarLayout
  2. android:id="@+id/appbarlayout"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:layout_marginStart="-10dp"
  6. android:layout_marginTop="-10dp"
  7. android:layout_marginEnd="-10dp"
  8. android:layout_marginBottom="-10dp"
  9. android:padding="5dp">
  10. <RelativeLayout
  11. android:id="@+id/viewProfile"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:gravity="center">
  15. <androidx.appcompat.widget.Toolbar
  16. android:id="@+id/toolbar"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content" />
  19. <ImageButton
  20. android:id="@+id/back_press"
  21. android:layout_width="40dp"
  22. android:layout_height="50dp"
  23. android:layout_alignParentStart="true"
  24. android:layout_centerVertical="true"
  25. android:layout_marginEnd="15dp"
  26. android:background="@drawable/back_press"
  27. android:scaleType="fitCenter"
  28. android:src="@drawable/ic_back" />
  29. <de.hdodenhof.circleimageview.CircleImageView
  30. android:id="@+id/friendpic"
  31. android:layout_width="55dp"
  32. android:layout_height="55dp"
  33. android:layout_marginStart="-5dp"
  34. android:layout_marginEnd="20dp"
  35. android:layout_toEndOf="@+id/back_press"
  36. android:src="@drawable/ic_user" />
  37. <TextView
  38. android:id="@+id/friendname"
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. android:layout_toEndOf="@+id/friendpic"
  42. android:fontFamily="@font/alegreya_sc_italic"
  43. android:text="Username"
  44. android:textSize="22sp" />
  45. <TextView
  46. android:id="@+id/status"
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:layout_below="@+id/friendname"
  50. android:layout_toEndOf="@+id/friendpic"
  51. android:text="offline"
  52. android:textSize="16sp" />
  53. </RelativeLayout>
  54. </com.google.android.material.appbar.AppBarLayout>

下面是java代码

  1. RelativeLayout relativeLayout;
  2. relativeLayout = findViewById(R.id.viewProfile);
  3. relativeLayout.setOnClickListener(v -> {
  4. Log.d("TAG", "onCreate: True");
  5. Intent intent1 = new Intent(ChatActivity.this, ViewFriendProfile.class);
  6. intent1.putExtra("friendId", friendId);
  7. startActivity(intent1);
  8. });

注意:我试过用在 AppBarLayout 但没起作用。
我该怎么办?有什么建议吗?

gxwragnw

gxwragnw1#

尝试以下两个建议
使relativelayout可单击 android:clickable="true" 禁用imagebutton(它是relativelayout的子视图)click事件 android:clickable="false"

3wabscal

3wabscal2#

这个整体看起来像一个工具栏和一些菜单。如果我猜是这样,只需使用oncreateoptionsmenu()创建配置文件菜单,并使用onoptionsitemselect()侦听器重定向到该页面。如果不是这样,请忽略我的评论

相关问题