Shape drawable in layer-list rendered incorrectly for APIs below 23

僤鯓⒐⒋嵵緔 提交于 2019-11-30 09:04:24
0X0nosugar

First of all, I'd like to thank @pskink for helping me to analyse the problem :)

The xml in the question worked for API level 23+, because

it is possible to set width and height right within the "item" tag

like @android developer states in this SO post

It did not work for lower versions, because there one needs to set width and height attributes for a ShapeDrawable using the size tag.

I suppose Android needs the size to figure out how to scale the drawable (-> aspect ratio!) while taking into account the insets (top, left, right, bottom).

And as is sometimes the case with malformed (for a specific API level) xml, Android failed silently and simply did not draw the shape.

So the solution is:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:width="80px" android:height="80px"
        android:top="0px" android:left="0px" 
        android:right="0px" android:bottom="0px">
        <shape android:shape="rectangle">
            <solid android:color="@color/deep_orange"/>
        </shape>
    </item>
    <item android:width="60px" android:height="60px"
        android:top="10px" android:left="10px" 
        android:right="10px" android:bottom="10px">
        <shape android:shape="oval">
            <solid android:color="@color/light_yellow"/>
            <size android:height="60px" android:width="60px"/>
        </shape>
    </item>
</layer-list>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!