Understanding Android's <layer-list>

94,627

Solution 1

The values for left, top, right and bottom are measured from their respective edge.

So left=0dp, top=0dp, bottom=0dp & right=50dp will give you a rectangle that is (match_parent - 50dp) wide and not 50dp wide. Therefore larger values for "right" will actually give you a smaller rectangle.

The same would apply to the other value, but these would behave as expected in most cases, its just "right" that might cause confusion.

Solution 2

Either you use px instead of dp or multiply all dimensions by 10.

I'm ashamed to admit that I don't exactly know WHY this is happening but my guess is that it has something to do with densities where 1dp is a floating px value and the ImageView is scaled up.

Expert answer is welcomed :)

Share:
94,627
rekire
Author by

rekire

Check also my gists and projects on GitHub. Voice related Apps: REWE✝ (Google, Amazon) CentralStation CRM (Google (DE), Google (US), Amazon (DE), Amazon (US)) List of my Apps or Apps I worked on: snabble by snabble CentralPlanner by 42he (flutter) gooods by snabble toom by snabble CentralStation CRM by 42he Zykluskalender✝ by NetMoms Hotel Search by HRS REWE Lieferservice, Supermarkt by REWE Digital BILLA Online Shop✝ by REWE Digital CentralStation CRM by 42he ✝ = Reached end of life Open source projects where I am involved: Author of Konversation intent-schema-generation provisioning cli kotlin-multi-platform Contributor of dialog dialogflow alexa kotlin Author of LazyWorker android tool Contributor of Futter flutter You want to hire me? Check my careers profile.

Updated on July 08, 2022

Comments

  • rekire
    rekire almost 2 years

    I don't understand how the layer-lists work. I read the official documentation with some examples but it does not work for me like expected. I want four squares which should be padded with 1dp, but nothing is like expected. Here is a screenshot scaled by 500%:

    My suares
    (The wrong colors do not matter)
    As you can see the size is completely wrong and the paddings are missing. I tried to set real values like width/height and right/left/top/buttom to be sure that android get the point what I want.

    Here is my xml:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:top="0dp" android:left="0dp" android:bottom="0dp" android:right="0dp">
            <shape android:shape="rectangle">
                <size android:width="9dp"
                    android:height="9dp"/>
                <solid android:color="#f000"/>
            </shape>
        </item>
    
        <item android:top="1dp" android:left="1dp" android:bottom="5dp" android:right="5dp">
            <shape android:shape="rectangle">
                <size android:width="3dp"
                    android:height="3dp"/>
                <solid android:color="#f00"/>
            </shape>
        </item>
        <item android:top="1dp" android:left="5dp" android:bottom="5dp" android:right="1dp">
            <shape android:shape="rectangle">
                <size android:width="3dp"
                    android:height="3dp"/>
                <solid android:color="#0f0"/>
            </shape>
        </item>
    
        <item android:top="5dp" android:left="1dp" android:bottom="1dp" android:right="5dp">
            <shape android:shape="rectangle">
                <size android:width="3dp"
                    android:height="3dp"/>
                <solid android:color="#0f0"/>
            </shape>
        </item>
        <item android:top="5dp" android:left="5dp" android:bottom="1dp" android:right="1dp">
            <shape android:shape="rectangle">
                <size android:width="3dp"
                    android:height="3dp"/>
                <solid android:color="#f00"/>
            </shape>
        </item>
    </layer-list>
    
  • rekire
    rekire over 11 years
    You are right, but I would like to be dpi independed. Could you provide some more informations like if I really need all values of the rect (top,left,right,bottom) and an idea why dp seems not to be work in eclipse.
  • rekire
    rekire over 10 years
    I just found this answer again finally I used px and used for other screensizes (ldpi, mdpi, hdpi, xhdpi and xxhdpi) other values.
  • The Demz
    The Demz over 9 years
    Its like padding in html, right=50dp adds a padding of 50dp to the right.