无法禁用带有子视图的LinearLayout点击

时间:2017-06-03 11:40:39

标签: android android-linearlayout onclicklistener

所以我有这个XML结构:

<LinearLayout
            android:id="@+id/action_info"
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="0.25"
            android:orientation="vertical"
            android:padding="@dimen/padding_activity_map_bottom_dialog_buttons"
            android:layout_marginBottom="@dimen/margin_bottom_activity_map_bottom_dialog_content"
            android:clickable="true"
            android:background="?attr/selectableItemBackground">

            <ImageView
                android:src="@drawable/ic_action_info"
                android:layout_width="@dimen/dimens_activity_map_bottom_dialog_button_image"
                android:layout_height="@dimen/dimens_activity_map_bottom_dialog_button_image"
                android:layout_gravity="center"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/label_activity_map_info"
                android:textSize="@dimen/size_activity_map_bottom_dialog_marker_actions_button_label"
                android:gravity="center"/>

        </LinearLayout>

尝试禁用LinearLayout点击(通过设置enable = false):

private void disableEnableControls(boolean enable, ViewGroup vg){
        for (int i = 0; i < vg.getChildCount(); i++){
            View child = vg.getChildAt(i);
            child.setEnabled(enable);
            child.setClickable(enable);
            if (child instanceof ViewGroup){
                disableEnableControls(enable, (ViewGroup)child);
            }
        }
    }

但问题仍然存在,仍然点击了LinearLayout触发器。

2 个答案:

答案 0 :(得分:2)

解决了改变

private void disableEnableControls(boolean enable, ViewGroup vg){
        for (int i = 0; i < vg.getChildCount(); i++){
            View child = vg.getChildAt(i);
            child.setEnabled(enable);
            child.setClickable(enable);
            if (child instanceof ViewGroup){
                disableEnableControls(enable, (ViewGroup)child);
            }
        }
    }

private void disableEnableControls(boolean enable, ViewGroup vg){
        vg.setEnabled(enable); // the point that I was missing
        for (int i = 0; i < vg.getChildCount(); i++){
            View child = vg.getChildAt(i);
            child.setEnabled(enable);
            child.setClickable(enable);
            if (child instanceof ViewGroup){
                disableEnableControls(enable, (ViewGroup)child);
            }
        }
    }

答案 1 :(得分:0)

请删除此行并检查您是否按预期获得输出android:clickable =&#34; true&#34;