Skip to content Skip to sidebar Skip to footer

What Is Wrong With My Layer-list Drawable?

I wanted to set a custom drawable to be the android:src of FloatingActionButton in this layout:

Solution 1:

Although it is syntactically possible to use dp values, this seems to cause problems with some density categories, so better use px instead. Shape drawables will be scaled to fit their containing View anyway, the values you provide just have an influence on the proportions of the shape.

For your layer-list drawable, you have to provide a common "frame" for the size of all the shape drawables as each layer will be scaled to fit the View independently of the others.

This means that the smaller circles need some additional information about how far from the edge they should be drawn. For a circle centered in its container:

distance = (container_size - circle_size) / 2

If we take 150px as the basic size for all the drawables, then for example the pink circle (size 100px) needs 25px distance to the edges of its container.

You can provide this information by setting attributes to each item:

<itemandroid:top="25px"android:left="25px"android:bottom="25px"android:right="25px"><shapeandroid:shape="oval"><solidandroid:color="#FC00FF" /><!-- pink --><sizeandroid:width="100px"android:height="100px"/></shape></item>

Note that it's important to have attributes not only for top/ left but also for bottom/ right. If you leave out any attributes then the drawable will be scaled to touch the respective edge, so the circle shape could be rendered as an oval or as an off-center circle.

Post a Comment for "What Is Wrong With My Layer-list Drawable?"