javaandroidxmldrawablelayer-list

How does LayerList xml interrupted in android?


In the official site Layer List explanation there is an example that I can't understand.

The example:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/android_red" />
    </item>
    <item
        android:left="10dp"
        android:top="10dp">
        <bitmap
            android:gravity="center"
            android:src="@drawable/android_green" />
    </item>
    <item
        android:left="20dp"
        android:top="20dp">
        <bitmap
            android:gravity="center"
            android:src="@drawable/android_blue" />
    </item>
</layer-list>

The output:

Output

I can't understand two things:

  1. Why the Red android isn't in the center. He is the only one who doesn't have top or left offset. Maybe I don't understand the meaning of top and left offset, From which point the offset is calculated?

  2. Does android: gravity="center" is in relation to all containers or in relation to the item above ("his sibling), i.e., does the second item is in relation to the container or to the first item?


Solution

  • i think if you copy this code and try to explore it you can get the idea. in fact the red android is in the center but it is not clear in this picture , so if you put it in imageView it will look like this

    i gave background yellow color to be clear , so you can see that the red android is in the center.

    2- android: gravity="center" is for the item that contain it, notice that the item will stretch to the parent width and height so you can notice for example that the blue android is in the center of its item which its left top corner is (10,10) and its bottom right corner is the same as the parent