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

4

31

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

Pirozzo answered 8/2, 2014 at 23:6 Comment(0)
M
72

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.

Methionine answered 8/2, 2014 at 23:49 Comment(1)
if your solid color come in front of bitmap set bit map after the 2nd item tag. <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:bottom="20dp" android:left="40dp" android:right="40dp" android:top="20dp"> <shape android:shape="rectangle"> <solid android:color="@color/colorPrimary" /> </shape> </item> <item> <bitmap android:src="@drawable/splash_screen_logo" /> </item> </layer-list>Digitalism
P
17
<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="oval">
            <solid android:color="#FFE3F5F2"/>
        </shape>
    </item>

    <item
        android:bottom="7dp"
        android:left="7dp"
        android:right="7dp"
        android:top="7dp">
        <bitmap
            android:src="@drawable/ic_link">
        </bitmap>

    </item>


</layer-list>
Pathoneurosis answered 4/11, 2018 at 8:14 Comment(0)
C
7

Wrap the bitmap inside an inset drawable and set your padding there

Cystine answered 8/2, 2014 at 23:20 Comment(1)
can you give an example?Finney
P
5

If you add the drawable to the same item in the layer-list you can apply properties such as android:top, android:bottom, android:left or android:right to handle padding.

I fixed my issue with the drawable being out of alignment with the TextView by simply using the android:width and android:height properties.

E.g.

<item
    android:width="26dp" android:height="26dp"
    android:drawable="@drawable/ic_person">
    <shape android:shape="rectangle" >
        <solid android:color="@android:color/transparent" />
    </shape>
</item>

Piperpiperaceous answered 11/4, 2016 at 13:13 Comment(1)
Width and Height attributes in layer-list items is only available in API 23 unfortunately.Desdemona

© 2022 - 2024 — McMap. All rights reserved.