【问题标题】:android api: child view is still visible with a parent view set to INVISIBLEandroid api:子视图仍然可见,父视图设置为不可见
【发布时间】:2014-09-09 16:19:08
【问题描述】:

我的应用中有 3 个片段。第三个片段的布局有以下xml:

    <TableRow
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:weightSum="1"
    android:id="@+id/settings_btns_container"
    android:visibility="invisible"
    >

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center_horizontal|bottom"
            android:layout_weight="0.5">

            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Cancel"
                android:id="@+id/cancel"/>
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center_horizontal|bottom"
            android:id="@+id/linearLayout"
            android:layout_weight="0.5">

            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Save"
                android:id="@+id/save"/>
        </LinearLayout>
    </LinearLayout>

</TableRow>

settings_btns_container tablerow 设置为不可见,因此它不会在初始加载时显示。当 onCreate 被调用时,一切都按预期工作。但是,当片段超出范围时(选择了第一个片段),当我们回到第三个片段时,这个 tablerow 容器的子项变得可见。我在 onCreate() 中添加了以下显式代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
btn_container_ui = (TableRow) settings_view.findViewById(R.id.settings_btns_container);
/*
some other code
...
*/
btn_container_ui.setVisibility(View.INVISIBLE);

无论我将其设置为 INVISIBLE 还是 GONE 都无关紧要,孩子们仍然可见。我检查了父 tablerow 的值,它设置为 4,这是 INVISIBLE 常量的值。将孩子自己设置为 INVISIBLE 隐藏它们没有问题,但我想隐藏整个父母,所以我不必一个一个地通过孩子。我在这里遗漏了什么吗?

【问题讨论】:

  • 我不认为让父母不可见意味着孩子也会变得不可见,我认为您必须将每个孩子都设置为不可见
  • 为什么需要为每个子视图一一设置@TomTsagk

标签: android xml view children invisible


【解决方案1】:

我不确定这是否有帮助,但如果视图及其所有祖先都可见,View 类的 isShown() 方法会返回 true。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-25
    • 2013-11-15
    • 2023-03-05
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多