禁用布局中的所有子视图

时间:2013-11-05 22:34:43

标签: java android childviews

在发布我的问题之前,我看到了许多与此相关的线程。但没有一个对我有用。我有一个RelativeLayout与许多其他布局和片段作为孩子。我想在单击按钮时禁用“content_view”的所有子项以及content_view本身。我试过了

contentView.setDisabled(false);

这不起作用。我也试过了

for (int i = 0; i < layout.getChildCount(); i++) {
    View child = layout.getChildAt(i);
    child.setEnabled(false);
}

即使这样也行不通。我究竟做错了什么?请在下面找到我的.xml代码。 我甚至尝试在所有视图上方放置一个视图。即便这样也无法解决我的问题。

<RelativeLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:id="@+id/content_view"
    android:background="#ffffff">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/channel_actionbar"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp"
        android:duplicateParentState="true">

        <FrameLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:id="@+id/body_container"
            android:layout_below="@+id/channel_actionbar"
            android:duplicateParentState="true">

            <RelativeLayout
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:id="@+id/channelList"
                android:duplicateParentState="true">

                <com.mobile.subview.ScrollViewWithScrollListener
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/scrollView"
                    android:layout_alignParentTop="false"
                    android:duplicateParentState="true">

                    <RelativeLayout
                        android:orientation="vertical"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:duplicateParentState="true">

                        <FrameLayout
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:id="@+id/imagePlaceHolder"
                            android:duplicateParentState="true"></FrameLayout>

                        <TableLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:id="@+id/channelTable"
                            android:layout_below="@+id/imagePlaceHolder"
                            android:duplicateParentState="true"></TableLayout>

                        <com.mobile.subview.CustomTextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="You do not qualify for any engagements or channels.  Please check back later."
                            app:typeface="fonts/HelveticaNeue"
                            app:customStyle="Regular"
                            android:id="@+id/noChannelsMessage"
                            android:textColor="#000"
                            android:textSize="@dimen/contentTextSize"
                            android:visibility="gone"
                            android:duplicateParentState="true"/>
                    </RelativeLayout>
                </com.mobile.subview.ScrollViewWithScrollListener>

                <com.mobile.subview.ParallaxImage
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/header"
                    android:adjustViewBounds="true"
                    android:layout_alignParentTop="true"
                    android:scaleType="fitStart"
                    android:visibility="invisible"
                    android:duplicateParentState="true"/>
            </RelativeLayout>

            <RelativeLayout
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:layout_below="@+id/channel_actionbar"
                android:id="@+id/sibling_view"
                android:visibility="gone"
                android:duplicateParentState="true"></RelativeLayout>

        </FrameLayout>
    </LinearLayout>

    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="invisible"
        android:id="@+id/retailersContainer"
        android:layout_marginLeft="0dp"
        android:duplicateParentState="true"
        android:layout_below="@+id/channel_actionbar">

        <fragment
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:name="com.mobile.subview.List"
            android:id="@+id/retailers"
            android:duplicateParentState="true"/>

        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/retailers"
            android:id="@+id/retailerClickBlocker"
            android:duplicateParentState="true"></FrameLayout>
    </RelativeLayout>

    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="@dimen/actionBarHeight"
        android:layout_alignParentTop="true"
        android:id="@+id/channel_actionbar"
        android:background="#F8F8F8"
        android:layout_marginLeft="0dp"
        android:duplicateParentState="true">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/shared_navmenu_button"
            android:id="@+id/show_menu_button"
            android:layout_centerVertical="true"
            android:background="@null"
            android:scaleType="fitCenter"
            android:layout_marginLeft='5px'
            android:duplicateParentState="true"/>

        <com.mobile.subview.CustomTextView
            android:id="@+id/channel_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textSize="@dimen/titleTextSize"
            android:textColor="#000"
            android:text="Test Title"
            app:typeface="fonts/HelveticaNeue"
            app:customStyle="Medium"
            android:visibility="gone"
            android:duplicateParentState="true"/>


        <ImageView
            android:id="@+id/logo"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/shared_navbar_logo2x"
            android:visibility="gone"
            android:scaleType="fitCenter"
            android:duplicateParentState="true"/>

        <com.mobile.subview.CustomButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:id="@+id/channel_done_btn"
            app:typeface="fonts/HelveticaNeue"
            app:customStyle="Regular"
            android:visibility="gone"
            android:duplicateParentState="true"/>

        <com.mobile.subview.CustomButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            app:typeface="fonts/HelveticaNeue"
            app:customStyle="Regular"
            android:id="@+id/channel_share_btn"
            android:visibility="gone" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="#D6D6D6"
            android:id="@+id/divider"
            android:layout_alignParentBottom="true"
            android:duplicateParentState="true"/>

    </RelativeLayout>

    <View
        android:visibility="gone"
        android:id="@+id/click_preventing_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#66000000"
        android:clickable="false" />

</RelativeLayout>

4 个答案:

答案 0 :(得分:25)

由于您的布局是如此严重嵌套,因此您需要以递归方式禁用视图。不要使用你的方法,尝试这样的事情:

private static void disable(ViewGroup layout) {
    layout.setEnabled(false);
    for (int i = 0; i < layout.getChildCount(); i++) {
        View child = layout.getChildAt(i);
        if (child instanceof ViewGroup) {
            disable((ViewGroup) child);
        } else {
            child.setEnabled(false);
        }
    }
}

然后致电:

disable(content_view);

答案 1 :(得分:3)

即使答案是预期的而不是使用递归,我认为下面的代码也可以解决这个问题。这就是我以前用来解除它的原因。我刚刚通过了parentlayout以及是否显示或隐藏为布尔参数

private void disable(LinearLayout layout, boolean enable) {
        for (int i = 0; i < layout.getChildCount(); i++) {
            View child = layout.getChildAt(i);
            child.setEnabled(enable);
            if (child instanceof ViewGroup) {
                ViewGroup group = (ViewGroup) child;
                for (int j = 0; j < group.getChildCount(); j++) {
                    group.getChildAt(j).setEnabled(enable);
                }
            }

        }

答案 2 :(得分:0)

以下是Kotlin版本:

使用Android KTX

<div [perfectScrollbar]="config" [ngStyle]="{ 'max-height': '60vh', position: 'relative' }">
.
.
.
.
</div>

没有Android KTX

fun View.changeEnableChildren(enable: Boolean) {
    isEnabled = enable
    (this as? ViewGroup)?.let {
        forEach {
            changeEnableChildren(enable)
        }
    }
}

注意:递归方法可能会导致StackOverFlowError ,在这种情况下,您应将fun View.changeEnableChildren(enable: Boolean) { isEnabled = enable (this as? ViewGroup)?.let { for (i in 0..it.childCount) { changeEnableChildren(enable) } } } 替换为changeEnableChildren(enable)

答案 3 :(得分:0)

android:duplicateParentState="true"应用于孩子