【问题标题】:How to set drawable size in layer-list如何在图层列表中设置可绘制大小
【发布时间】:2011-10-29 14:53:25
【问题描述】:

我在图层列表中设置可绘制对象的大小时遇到​​问题。我的 XML 如下所示:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/menu_drawable1"/>
    <item android:drawable="@drawable/menu_drawable2"/>       
</layer-list>

Drawable1 比 Drawable2 大,所以 Drawable2 被调整大小。如何覆盖它并设置 Drawable2 的原始大小?我不希望它调整大小。我尝试设置宽度和高度,但这不起作用。而且drawables实际上不是位图,它们是选择器,所以我不能使用位图技巧。


看起来我已经通过使用“左”和“右”获得了一半的解决方案,但是,这仍然不能让我自动设置图像的确切大小。现在我的 xml 看起来像这样:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/menu_drawable1"/>
    <item android:drawable="@drawable/menu_drawable2" android:right="150dp"/>       
</layer-list>

【问题讨论】:

  • 我真的很想知道你提到的位图技巧 :) 你的意思是在项目标签中嵌入位图吗?
  • 我也觉得这个问题仍然很相关。虽然你写在底部的技巧有帮助。

标签: android list android-layout size drawable


【解决方案1】:

您可以使用具有所需值的 android:widthandroid:height 参数。

下面是示例可绘制图层列表和屏幕截图。我使用了来自谷歌material icons 的矢量drawable 和自己的选择器drawable。

原始 circle.xml 可绘制形状的大小为 100dp x 100dp,但在 drawable.xml 中,我通过 android:widthandroid:height 将此值覆盖为 24dp x 24dp参数。

希望对你有所帮助!

drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/circle" android:height="24dp" android:width="24dp"/>
    <item android:drawable="@drawable/ic_accessibility_black_24dp"/>
    <item android:drawable="@drawable/ic_http_white_24dp" android:gravity="center" android:top="-3dp" android:height="5dp" android:width="5dp"/>
</layer-list>

circle.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:innerRadius="20dp" android:shape="oval">
            <solid android:color="#CCC" />
            <size android:width="100dp" android:height="100dp"/>
        </shape>
    </item>
</selector>

【讨论】:

  • 免责声明:宽度和高度为 API 23+
  • 下面的 23 用什么?
【解决方案2】:

您可以为此使用插入标签。 例如

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/menu_drawable1"/>
   <item>
       <inset android:drawable="@drawable/menu_drawable2"
           android:insetRight="50dp"
           android:insetTop="50dp"
           android:insetLeft="50dp"
           android:insetBottom="50dp" />
   </item>

这将在您的 menu_drawable2 周围创建填充,因此它不会被缩放。只需设置您需要的尺寸即可。

【讨论】:

  • 如果您使用多个插图,但它似乎会累积。确实非常令人不安。
  • 这对我来说使用矢量 Drawable 效果很好,实际上没有其他方法。
【解决方案3】:

我在 Android 5.0 (API 21) 上也遇到过这个问题。看来the gravity of item doesn't work for API less than 23。所以布局需要更新,我通过为应该是右上对齐覆盖的第二个项目节点应用计算的填充使其兼容。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_svg_filter_black"/>

    <item
        android:left="16dp"
        android:right="0dp"
        android:top="0dp"
        android:bottom="16dp"
        android:drawable="@drawable/ic_circle_dot_red"
    />
</layer-list>

【讨论】:

    【解决方案4】:

    希望这会有所帮助:-
    您可以为menu_drawable2 使用九个可绘制补丁。你可以让menu_drawable2 像在它的边缘周围有透明区域一样。使用此透明区域指定九个补丁中的可拉伸区域。

    【讨论】:

      【解决方案5】:

      对于低于 23 的 API 级别,您可以在 &lt;shape&gt; 对象上指定 &lt;size&gt;,如下所示:

      <?xml version="1.0" encoding="utf-8"?>
      <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
      
          <item>
              <shape android:shape="oval" android:tint="@color/darkDeepBlue">
                  <padding android:right="2dp" android:top="2dp" android:bottom="2dp" android:left="2dp"/>
              </shape>
          </item>
      
          <item>
              <shape android:shape="oval" android:tint="@color/sunnyYellow">
                  <size android:width="8dp" android:height="8dp"/>
              </shape>
          </item>
      
      </layer-list>
      

      【讨论】:

        猜你喜欢
        • 2012-03-25
        • 2011-07-04
        • 2013-01-21
        • 2014-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-18
        相关资源
        最近更新 更多