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

后端 未结 1 1208
花落未央
花落未央 2020-12-14 21:39

Yesterday, I\'ve switched to Android Studio version 2.1.3. Today, I\'ve re-opened one of my projects and tried to add some layer-list drawables for use with an ImageVi

相关标签:
1条回答
  • 2020-12-14 22:07

    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>
    
    0 讨论(0)
提交回复
热议问题