Android 如何向位图和图层列表中的颜色添加填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21652917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:52:59  来源:igfitidea点击:

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

androidandroid-layoutbitmapshapelayer-list

提问by learner

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。谢谢你的帮助。

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

由于这个配置不起作用,我的猜测是将填充放在第一层。但我不确定如何使用位图来做到这一点

回答by chiuki

Move the bottom padding from the shapeto the parent item:

将底部填充从 移动shape到父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-listfor more info.

有关更多信息,请参阅http://idunnolol.com/android/drawables.html#layer-list

回答by Farshid roohi

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

回答by dangVarmit

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

将位图包裹在 inset drawable 中并在那里设置填充

回答by Arjun Shankar

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

如果您在图层列表中的绘制添加到同一个项目,你可以申请,如性能android:topandroid:bottomandroid:leftandroid:right处理填充。

I fixed my issue with the drawable being out of alignment with the TextView by simply using the android:widthand android:heightproperties.

我通过简单地使用android:widthandroid:height属性解决了可绘制对象与 TextView 不对齐的问题。

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>