我在现在这个项目开发的过程中,遇到了ImageButton点击失效的问题,百思不得其解,甚至开始怀疑人生了,终于发现问题的原因,先上布局代码.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/video_view_bg_color"
android:orientation="vertical"
tools:context=".activity.ShareActivity">
<com.rinogame.aiduke.view.TitleView
android:id="@+id/view5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
app:text=""/>
<RelativeLayout
android:id="@+id/relative_play"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/view5"
android:layout_margin="@dimen/margin_5dp"
android:background="@drawable/common_round_corner">
<org.wysaid.view.VideoPlayerGLSurfaceView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="center_horizontal"
android:layout_margin="@dimen/margin_8dp"/>
<ImageView
android:id="@+id/save_activity_btn_play"
android:layout_width="match_parent"
android:layout_height="@dimen/height_300dp"
android:layout_gravity="center"
android:background="@drawable/common_round_corner"
android:visibility="visible"/>
</RelativeLayout>
<FrameLayout
android:id="@+id/frame_input"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/linear_share"
android:layout_below="@+id/relative_play"
android:background="#1b242D"
>
<android.support.design.widget.TextInputLayout
android:id="@+id/save_activity_ti"
android:layout_width="match_parent"
android:layout_height="@dimen/height_100dp"
android:layout_margin="@dimen/margin_8dp"
android:background="@drawable/common_round_corner"
app:hintTextAppearance="@style/hintStyle">
<EditText
android:id="@+id/save_activity_tv_share_content"
android:layout_width="match_parent"
android:layout_height="@dimen/height_150dp"
android:layout_gravity="center_horizontal|top"
android:layout_margin="@dimen/margin_8dp"
android:background="@null"
android:gravity="top"
android:hint="添加描述~"
android:maxLength="120"
android:textColor="@color/share_text_color"
android:textCursorDrawable="@drawable/main_edit_cursor_bg"/>
</android.support.design.widget.TextInputLayout>
<TextView
android:id="@+id/save_activity_tv_left_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="@dimen/margin_20dp"
android:layout_marginRight="@dimen/margin_20dp"
android:text="还能输入120字"
android:textColor="@color/grey"
android:textStyle="bold"/>
</FrameLayout>
<LinearLayout
android:id="@+id/linear_share"
android:layout_width="match_parent"
android:layout_height="@dimen/height_60dp"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary"
android:orientation="vertical">
<!--留意这个ImageButton的Id-->
<ImageButton
android:id="@+id/share_btn_share"
android:layout_width="@dimen/width_50dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:background="@null"
android:scaleType="fitCenter"
android:src="@mipmap/title_button_share_bg"/>
</LinearLayout>
</RelativeLayout>
点击失效的是ID为share_btn_share的ImageButton,正常看这个页面没有任何问题,ImageButton按照正常的setOnClickListener却无法响应点击,注意顶部是一个自定义的TitleView的符合控件,实现Toolbar的作用.内部也有ImageButton,在我重命名这个TitleView的ImageButton的时候,我发现上面布局里的ImageButton在Activity里findViewById的id也被修改了!这说明TitleView内部的ImageButton和布局里的ImageButton的Id重复了,导致Android Studio无法准确找到响应点击的ImageButton