简体   繁体   中英

android api: child view is still visible with a parent view set to INVISIBLE

I have 3 fragments in my app. The layout of the 3rd fragment has the following 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>

The settings_btns_container tablerow is set invisible so it doesn't show up on initial load. When onCreate is called, everything works as expected. However, when the fragment goes out of scope (1st fragment is selected), when we come back to the 3rd fragment, the children of this tablerow container become visible. I have added the following explicit code in 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);

Whether I set it to INVISIBLE or GONE is irrelevant, the children are still visible. I checked the value of the parent tablerow and it is set to 4, which is the value of INVISIBLE constant. Setting children themselves to INVISIBLE hides them with no problem, but I wanted to hide the entire parent, so I don't have to go through children one by one. Am I missing something here?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM