相对布局不可点击

时间:2017-04-25 05:30:19

标签: android android-relativelayout

下面的

是我的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并添加了点击监听器到相对布局..但它没有点击事件..而是点击事件正在生成,而我点击相对布局内的textview。

可能是什么问题?

感谢。

2 个答案:

答案 0 :(得分:1)

尝试让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" >

答案 1 :(得分:1)

click事件从Child传递给父级。如果任何孩子是可点击的,那么第一个孩子将获得点击事件,如果孩子不可点击,则点击事件将传递给父母。

如果父级想要在子级之前单击事件,则需要在父视图类中覆盖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>