Search code examples
androidandroid-layoutbitmapshapeslayer-list

How do I add padding to a bitmap and a color in a layer-list


I need to create a drawable with a layer-list such that the background layer is an image and the foreground is a transparent color. I am able to do that. Where I am not succeeding is adding some padding/margin so that at them bottom a piece of the images shows without the colored layer on top of it. Here is my xml. Thanks for helping.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <bitmap android:src="@drawable/img1" 
        />
  </item>

  <item>
    <shape
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle" >
      <solid android:color="@color/color1" />
      <padding android:bottom="20dp" />
    </shape>
  </item>

</layer-list>

Since this configuration is not working, my guess is to put the padding in the first layer. But I am not sure how to do that with a bitmap


Solution

  • Move the bottom padding from the shape to the parent item:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
      <item>
        <bitmap android:src="@drawable/img1" />
      </item>
      <item android:bottom="20dp">
        <shape android:shape="rectangle" >
          <solid android:color="@color/color1" />
        </shape>
      </item>
    </layer-list>
    

    See http://idunnolol.com/android/drawables.html#layer-list for more info.