SqueezyMo
SqueezyMo

Reputation: 1726

Z-ordering of child views with respect to their parent

Background

In my app I have a lot of fragments that share the same static overlay. Each fragment can be resized dynamically (i.e. its weight may change) so I want the size of my overlay to be in sync with it. Therefore it makes sense to either make them siblings or, as it occurred to me, put one inside another. The first approach works fine but it implies introducing one extra ViewGroup which seems redundant (or does it not?). The latter is the one I'm having problems with.

Problem

Consider these two layouts.

Container R.id.container is where I put my fragment in runtime with FragmentManager. Fragment R.id.overlay is my overlay.

The difference is which one is the parent.

Layout A.

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment class="com.example.OverlayFragment"
        android:id="@+id/overlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

Layout B.

<fragment class="com.example.OverlayFragment"
    android:id="@+id/overlay"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</fragment>

In both cases my overlay ends up below container (by z-axis). That is, container keeps overlapping it regardless of its role in hierarchy.

What are the rules for defining views's z-order in situations like this? Is it just the order of their initialization? I couldn't google it out.

Thanks.

Upvotes: 0

Views: 416

Answers (1)

user5703518
user5703518

Reputation:

From documentation:

When the system creates this activity layout, it instantiates each fragment specified in the layout and calls the onCreateView() method for each one, to retrieve each fragment's layout. The system inserts the View returned by the fragment directly in place of the element.

So I think you should use something like this:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <fragment class="com.example.OverlayFragment"
        android:id="@+id/overlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

Upvotes: 1

Related Questions