2017-04-25 70 views
1

下面是我的XML:相对布局不能点击

 <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事件..而不是单击事件发生,而我点击在相对布局里面的textview。

什么可能是isssue?

谢谢。

+0

您是否希望textview和父级相对布局可点击或只有相对布局? – Avi

+0

你能展示你的java代码吗? –

+0

现在,在为relativelayout中的内部控件添加clickable和focusable false后,我必须单击twise而不是单击相对布局。 –

回答

1

的单击事件是由儿童传递给​​家长。如果任何孩子是可点击的,则第一个孩子将获得点击事件,如果孩子不可点击,则点击事件传递给父母。

如果父母想要孩子之前的点击事件,那么它需要覆盖父视图类中的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> 
+0

所以,你已经完成了.. clikable和focusable =“false”到chiild?它是更好的解决方案? –

+0

如果你不想让孩子点击,那么是吗? – Avi

+0

这不适合你吗? – Avi

0

尽量让你Relative Layout可聚焦:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/rl_simple_list_item_header" 
    android:orientation="horizontal" 
    android:focusable="true" 
    android:focusableInTouchMode="true" > 
+0

不工作的兄弟.. –

+0

有没有这种相对布局的父母布局?你可以显示你的完整xml –