我在如今這個項目開發的過程當中,遇到了ImageButton點擊失效的問題,百思不得其解,甚至開始懷疑人生了,終於發現問題的緣由,先上佈局代碼.android
<?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沒法準確找到響應點擊的ImageButtonapp