Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Android's <layer-list>

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> 
like image 799
rekire Avatar asked Jan 21 '13 10:01

rekire


People also ask

What is layered list in android?

Layer list. A LayerDrawable is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top.

How do I add text to a layer list?

One way to add Texts in your drawable layer-list is by creating a png file of the text and adding it using bitmap.

What is use level in android?

Basically, useLevel makes it so the drawable can be drawn partially. For example there is a method ImageView. setImageLevel() that lets you set a "level," e.g. 25% progress, so the ring drawable would be drawn as a quarter-circle.

How do you add a drawable to a Layerlist?

To define a LayerDrawable object, you need to add an XML file under the app/res/drawable folder. And the file name is just the drawable resource id. In this example, the app/res/drawable/my_layer_list. xml file contains a layer list definition.


2 Answers

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.

like image 74
Andrew Kelly Avatar answered Sep 19 '22 20:09

Andrew Kelly


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 :)

like image 31
OcuS Avatar answered Sep 16 '22 20:09

OcuS