【发布时间】:2022-01-24 07:14:40
【问题描述】:
我在图像视图上的 Click Listener 遇到了一些问题
如果单击按钮
,我会尝试记录一些文本
但它根本不起作用
这是我的代码,希望你能帮助我:
ChatHeadService.java
ImageView closeButton = (ImageView) mChatHeadView.findViewById(R.id.close_btn);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("myTag", "This is my message");
//close the service and remove the chat head from the window
stopSelf();
}
});
Layout_chat_head.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="65dp"
android:id="@+id/chat_head_root"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--Profile image for the chat head.-->
<ImageView
android:id="@+id/chat_head_profile_iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="8dp"
android:src="@drawable/icon_128"
tools:ignore="ContentDescription" />
<!--Close button-->
<ImageView
android:id="@+id/close_btn"
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_marginLeft="40dp"
android:src="@drawable/close"
tools:ignore="ContentDescription" />
</RelativeLayout>
P/s:我在这里读过类似的问题:Android ImageView's onClickListener does not work
但这似乎不适合我。
【问题讨论】:
-
您遇到的错误是什么?
-
这不是错误。就是不行,setOnClickListener() 不能识别点击
-
您确定日志显示正常吗?
-
是的,我测试过,日志工作正常。
-
您可以添加布局的屏幕截图吗?您没有为相对布局的子级定义任何对齐方式。