【问题标题】:Layer list gets cropped to drawable图层列表被裁剪为可绘制
【发布时间】:2019-07-10 14:55:30
【问题描述】:

这是我正在尝试构建的所需可绘制对象。

问题:

据我所知,图层列表的大小取决于其中的最后一项。但如果我在标签<item> 内使用android:drawable,图层列表将被裁剪为可绘制大小,无论它在什么位置。

这是我的swipe_close.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:innerRadius="0dp"
            android:shape="ring"
            android:thickness="34dp"
            android:useLevel="false">
            <solid android:color="#ffffff" />
        </shape>
    </item>
    <item android:drawable="@drawable/ic_close_swipe" />
    <item>
        <shape
            android:innerRadius="34dp"
            android:shape="ring"
            android:thickness="10dp"
            android:useLevel="false">
            <solid android:color="#E6E6E6" />
        </shape>
    </item>
</layer-list>

还有ic_close_swipe.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
  android:width="39dp"
  android:height="39dp"
  android:viewportWidth="39"
  android:viewportHeight="39">
    <path
        android:pathData="M37.7269,5.2568C38.5079,4.4757 38.5079,3.2094 37.7269,2.4283L36.5711,1.2726C35.7901,0.4916 34.5238,0.4916 33.7427,1.2726L20.7758,14.2395C19.9947,15.0206 18.7284,15.0206 17.9473,14.2395L4.9804,1.2726C4.1994,0.4916 2.933,0.4916 2.152,1.2726L0.9962,2.4283C0.2152,3.2094 0.2152,4.4757 0.9962,5.2568L13.9632,18.2237C14.7442,19.0048 14.7442,20.2711 13.9632,21.0521L0.9962,34.0191C0.2152,34.8001 0.2152,36.0665 0.9962,36.8475L2.152,38.0032C2.933,38.7843 4.1994,38.7843 4.9804,38.0032L17.9473,25.0363C18.7284,24.2553 19.9947,24.2553 20.7758,25.0363L33.7427,38.0032C34.5238,38.7843 35.7901,38.7843 36.5711,38.0032L37.7269,36.8475C38.5079,36.0665 38.5079,34.8001 37.7269,34.0191L24.7599,21.0521C23.9789,20.2711 23.9789,19.0048 24.7599,18.2237L37.7269,5.2568Z"
        android:fillColor="#C4C4C4"/>
</vector>

【问题讨论】:

    标签: android android-vectordrawable layer-list


    【解决方案1】:

    我建议更改swipe_close.xml 如下。

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape
                android:innerRadius="0dp"
                android:shape="ring"
                android:thickness="34dp"
                android:useLevel="false">
                <solid android:color="#ffffff" />
            </shape>
        </item>
        <item
            android:bottom="25dp"
            android:drawable="@drawable/ic_close_swipe"
            android:left="25dp"
            android:right="25dp"
            android:top="25dp" />
        <item>
            <shape
                android:shape="ring"
                android:thickness="10dp"
                android:useLevel="false">
                <solid android:color="#E6E6E6" />
            </shape>
        </item>
    </layer-list>
    

    希望有帮助!

    【讨论】:

    • 我所做的,但这不是一个合适的解决方案。谢谢。
    • 嗯...你使用了我提供的drawable吗?我在ic_close_swipe 周围添加了填充。你能解释一下为什么你认为这不是一个合适的解决方案吗?
    • 是的,我使用了填充,但是 24.5 而不是 25。这不好,因为这种填充取决于我使用的可绘制对象。所以如果我改变drawable,它会变小或变大,整个图层列表将被调整大小。我希望能够在 swipe_close.xml 中设置图层列表大小。再次感谢您的快速回复!
    • 感谢您的解释。但是,我认为您无法获得对每个可绘制对象都有效的动态。您必须在您的可绘制对象周围放置插图或填充,以将可绘制对象放入图层列表中。让我知道你是否发现了其他任何东西。祝你好运。 :)