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

℡╲_俬逩灬. 提交于 2019-12-03 01:19:51

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.

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

<?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>

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>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!