Question

The documentation of the XML resource specification of LayerList and LevelList drawables says that the item element will only accept bitmap as child element. On some answers concerning level lists, I see that people are adding shape tags as children to the item tags. I tried out the following and it seems to work nicely.

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:maxLevel="0">
    <shape android:shape="rectangle">
      <solid android:color="#ff0000" />
    </shape>
  </item>
  <item android:maxLevel="1">
    <shape android:shape="oval">
      <solid android:color="#0000ff" />
    </shape>
  </item>
</level-list>

Here is my question. Is this documented anywhere, or should I avoid this kind of construct because it is not supported in all API versions, or is the documentation simply incomplete and the above is perfectly legal?

To clarify, I know that I can specify any sort of drawable by using separate resource files and referring to them using the android:drawable attribute. The question refers to the inline specification of arbitrary drawables within the XML of the layer list or level list.

Was it helpful?

Solution 2

The answer is YES, it is allowed and supported behaviour.

While it is not documented, non-bitmap tags are used in the official Android XML resources. For example the source of progress_large_holo.xml contains the following.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <rotate
             android:drawable="@drawable/spinner_76_outer_holo"
             android:pivotX="50%"
             android:pivotY="50%"
             android:fromDegrees="0"
             android:toDegrees="1080" />
    </item>
    <item>
        <rotate
             android:drawable="@drawable/spinner_76_inner_holo"
             android:pivotX="50%"
             android:pivotY="50%"
             android:fromDegrees="720"
             android:toDegrees="0" />
    </item>
</layer-list>

Other sources also reveal that any XML that is accepted as a drawable resource can be placed inside the item tags.

OTHER TIPS

It doesn't state that it only accepts Bitmap objects, but that it will accept them. You can use the android:drawable to specify a drawable. Pull your shapes into their own drawable XML definition and reference them in the level-list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top