【问题标题】:How do I add padding to a bitmap and a color in a layer-list如何将填充添加到位图和图层列表中的颜色
【发布时间】:2014-03-06 08:24:45
【问题描述】:

我需要创建一个带有图层列表的可绘制对象,以便背景图层是图像,前景是透明颜色。我能够做到这一点。我没有成功的地方是添加一些填充/边距,以便在它们底部显示一块图像,而其顶部没有彩色层。这是我的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>

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

【问题讨论】:

    标签: android android-layout bitmap shape layer-list


    【解决方案1】:

    将底部填充从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>
    

    请参阅http://idunnolol.com/android/drawables.html#layer-list 了解更多信息。

    【讨论】:

    • 如果你的纯色出现在位图前面,则在第二个项目标签之后设置位图。 &lt;layer-list xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:bottom="20dp" android:left="40dp" android:right="40dp" android:top="20dp"&gt; &lt;shape android:shape="rectangle"&gt; &lt;solid android:color="@color/colorPrimary" /&gt; &lt;/shape&gt; &lt;/item&gt; &lt;item&gt; &lt;bitmap android:src="@drawable/splash_screen_logo" /&gt; &lt;/item&gt; &lt;/layer-list&gt;
    【解决方案2】:
    <?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>
    

    【讨论】:

      【解决方案3】:

      将位图包裹在一个可绘制的插图中并在那里设置您的填充

      【讨论】:

      • 你能举个例子吗?
      【解决方案4】:

      如果您将可绘制对象添加到图层列表中的同一项目,您可以应用android:top、android:bottom、android:left 或android:right 等属性来处理填充。

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

      例如

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

      【讨论】:

      • 不幸的是,图层列表项中的宽度和高度属性仅在 API 23 中可用。
      猜你喜欢
      • 1970-01-01
      • 2017-10-22
      • 1970-01-01
      • 2010-09-29
      • 2013-10-17
      • 2019-06-17
      • 2010-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多