Cooper Scott
Cooper Scott

Reputation: 706

Android - Stack XML Layouts ontop of each other

If you look at Instagram, they have posts stacked on posts, with each post containing comments, picture, publisher's username, etc.

Looks like a XML layout is reused/repeated throughout a screen/view.

Is that what they are doing? How can I do that?

Upvotes: 2

Views: 1236

Answers (2)

petey
petey

Reputation: 17140

Here is an example using a FrameLayout and some TextViews. All the views' inside this FrameLayout will appear at the (0,0) coordinate but have an offset found in the dimension of the childs individual view's android:layout_marginLeft/android:layout_marginStart and android:layout_marginTop attributes

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:text="11111111"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6dp"
        android:layout_marginTop="6dp"
        android:layout_marginStart="6dp" />


    <TextView
        android:text="2222222"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="12dp"
        android:layout_marginStart="12dp" />

    <TextView
        android:text="3333333"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="18dp"
        android:layout_marginStart="18dp" />

</FrameLayout>

And what it looks like:

And what it looks like

Upvotes: 1

Raanan
Raanan

Reputation: 4775

FrameLayout & RelativeLayout let you stack Views above each other, the z index is by the order they are added in code or written in xml. A layout is basically an extension of ViewGroup with rules, If you want to include another layout in a layout look into <include> and <merge> tags: http://developer.android.com/training/improving-layouts/reusing-layouts.html

Upvotes: 3

Related Questions