Android 相对布局不可单击
下面是我的xml:Android 相对布局不可单击,android,android-relativelayout,Android,Android Relativelayout,下面是我的xml: <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:orientation="horizontal" android:weightSum=
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal"
android:weightSum="2">
<RelativeLayout
android:clickable="true"
android:id="@+id/rel1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">
<com.app.thelist.view.CustomButton
android:clickable="false"
android:focusable="false"
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:text="btn1"
android:textAllCaps="false"
android:textColor="@color/txt_sub_title"
android:textSize="@dimen/txt_barselection_size"
app:FontEnum="regular" />
<com.app.thelist.view.CustomTextView
android:clickable="false"
android:focusable="false"
android:id="@+id/txt1"
style="@style/RegularFont"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/btn_my_drinks"
android:background="@drawable/border_gry_theme"
android:padding="@dimen/dimen_3"
android:text="00"
android:textColor="@color/txt_sub_title"
android:textSize="@dimen/txt_single_view_font" />
<ImageView
android:id="@+id/iv_bootm_selecter2"
android:layout_width="match_parent"
android:layout_height="7dp"
android:layout_below="@id/btn1"
android:scaleType="fitXY" />
</RelativeLayout>
<RelativeLayout
android:clickable="true"
android:id="@+id/rel1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">
<com.app.thelist.view.CustomButton
android:clickable="false"
android:focusable="false"
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:text="btn1"
android:textAllCaps="false"
android:textColor="@color/txt_sub_title"
android:textSize="@dimen/txt_barselection_size"
app:FontEnum="regular" />
<com.app.thelist.view.CustomTextView
android:clickable="false"
android:focusable="false"
android:id="@+id/txt1"
style="@style/RegularFont"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/btn_my_drinks"
android:background="@drawable/border_gry_theme"
android:padding="@dimen/dimen_3"
android:text="00"
android:textColor="@color/txt_sub_title"
android:textSize="@dimen/txt_single_view_font" />
<ImageView
android:id="@+id/iv_bootm_selecter2"
android:layout_width="match_parent"
android:layout_height="7dp"
android:layout_below="@id/btn1"
android:scaleType="fitXY" />
</RelativeLayout>
</LinearLayout>
我已经完成了findViewby id,并将click listener添加到了相对布局中。。但这并不是一个“点击事件”。。相反,当我在相对布局内单击textview时,正在生成click事件
问题可能是什么
谢谢。单击事件从子项传递到父项。如果任何子项可单击,则第一个子项将获得单击事件;如果该子项不可单击,则单击事件将传递给父项 如果父级希望在子级之前单击事件,则需要覆盖
onInterceptTouchEvent(MotionEvent ev)父视图类中的代码>
但如果你不想这样做,那么一个简单的解决方案就是让孩子不可点击。请在您的案例中查找已编辑的XML代码
<RelativeLayout
android:clickable="true"
android:id="@+id/rel1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:focusable="true"
android:focusableInTouchMode="true">
<com.app.thelist.view.CustomButton
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:text="btn1"
android:clickable="false"
android:focusable="false"
android:textAllCaps="false"
android:textColor="@color/txt_sub_title"
android:textSize="@dimen/txt_barselection_size"
app:FontEnum="regular" />
<com.app.thelist.view.CustomTextView
android:id="@+id/txt1"
style="@style/RegularFont"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/btn_my_drinks"
android:background="@drawable/border_gry_theme"
android:padding="@dimen/dimen_3"
android:text="00"
android:clickable="false"
android:focusable="false"
android:textColor="@color/txt_sub_title"
android:textSize="@dimen/txt_single_view_font" />
<ImageView
android:id="@+id/iv_bootm_selecter2"
android:layout_width="match_parent"
android:layout_height="7dp"
android:clickable="false"
android:focusable="false"
android:layout_below="@id/btn1"
android:scaleType="fitXY" />
</RelativeLayout>
您希望文本视图和父级相对布局都可单击还是仅相对布局?您可以显示java代码吗?现在,在将可单击和可聚焦false添加到我的relativelayout内部控件后。。我必须单击两次,而不是单击相对布局。@ZaptechDevKumar我已经在下面添加了我的答案。试试这个。那么,你已经做到了。。clikable and focusable=“false”到chiild?这是更好的解决方案?如果你不想让孩子点击,那么是吗?先生,我很抱歉,但是,我的相对布局在LinearLayout下无所谓任何东西都可以是relativelayout的父项,正如告诉你的,单击事件是从子项传递到父项的,在这种情况下,relativelayout是子项,而LinearLayout是父项,但你必须在relativelayout上处理单击事件,因此它将适用于你。因为relativelayout中没有其他子项是可单击的,因此使单击事件直接传递到相对布局。