java – 禁用布局中的所有子视图

在发布我的问题之前,我看到许多与此有关的线程.但没有一个为我工作.我有一个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>

解决方法

因为您的布局非常嵌套,您需要递归地禁用视图.而不是使用你的方法,尝试这样的:
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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


应用场景 C端用户提交工单、工单创建完成之后、会发布一条工单创建完成的消息事件(异步消息)、MQ消费者收到消息之后、会通知各处理器处理该消息、各处理器处理完后都会发布一条将该工单写入搜索引擎的消息、最终该工单出现在搜索引擎、被工单处理人检索和处理。 事故异常体现 1、异常体现 从工单的流转记录发现、
线程类,设置有一个公共资源 package cn.org.chris.concurrent; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * @Descrip
Java中的数字(带有0前缀和字符串)
在Java 9中使用JLink的目的是什么?
Java Stream API Filter(过滤器)
在Java中找到正数和负数数组元素的数量
Java 9中JShell中的不同启动脚本是什么?
使用Java的位填充错误检测技术
java中string是什么
如何使用Java中的JSON-lib API将Map转换为JSON对象?
Java菜单驱动程序以检查数字类型
使用Junit的Maven项目 - 检查银行账号
JAVA编程基础
在Java中使用throw、catch和instanceof来处理异常
在Java中,将数组分割为基于给定查询的子数组后,找到子数组的最大子数组和
如何在Java中从给定的字符串中删除HTML标签?
在PHP中,IntlChar getBlockCode()函数的翻译如下:
如何在Android中实现按下返回键再次退出的功能?
如何使用Java中的流式API解析JSON字符串?
Java中的模式类