首页 > 解决方案 > 图层列表内的可绘制矢量正在拉伸

问题描述

当我使用内部带有矢量可绘制对象的图层列表时,它在 API 23+ 上运行良好,但不是之前的。

我已启用vectorDrawables.useSupportLibrary = trueand AppCompatDelegate.setCompatVectorFromResourcesEnabled(true),但可绘制对象非常拉伸。

左边是奥利奥 26,右边是棒棒糖 21

左边是奥利奥 26,右边是棒棒糖 21

这是可绘制的

<layer-list>
            <item>
                <shape>
                    <stroke android:width="0.9dp" android:color="@color/colorPrimaryDark" />
                    <solid android:color="#FFF" />
                    <corners android:radius="16dp" />
                </shape>
            </item>

            <item android:drawable="@drawable/arrow_drawable" android:gravity="center_vertical|left" android:left="18dp" />
        </layer-list>

这就是应用可绘制对象的地方

<Spinner
        android:id="@+id/countrySpinner"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/spinner_background"
        android:popupBackground="@drawable/spinner_dropdown_background"
        android:popupElevation="4dp"
        app:popupTheme="@style/PopupTheme"
        app:layout_constraintBottom_toTopOf="@id/citySpinner"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.8" />

它在 ConstraintLayout 中,但我无法发布整个布局文件。

我可以做些什么来修复 Pre API 23 的拉伸?在代码中创建整个层列表会有所不同吗?

编辑:我之前确实尝试过,但忘记添加它。我确实尝试了以下xml

<layer-list>
            <item>
                <shape>
                    <stroke android:width="0.9dp" android:color="@color/colorPrimaryDark"/>
                    <solid android:color="#FFF"/>
                    <corners android:radius="16dp"/>
                </shape>
            </item>

            <item android:left="18dp">
                <bitmap android:src="@drawable/arrow_drawable" android:gravity="center_vertical|left"/>
            </item>
        </layer-list>

但这使得应用程序一开始说“android.content.res.Resources$NotFoundException: File res/drawable/spinner_background.xml from drawable resource ID #0x7f0700de这可能没有多大意义”就崩溃了。这就是 Android Studio 中的预览显示的内容

在此处输入图像描述

标签: androidxmlandroid-vectordrawable

解决方案


The documentation for layer-list drawables states:

All drawable items are scaled to fit the size of the containing View, by default. Thus, placing your images in a layer list at different positions might increase the size of the View and some images scale as appropriate. To avoid scaling items in the list, use a <bitmap> element inside the <item> element to specify the drawable and define the gravity to something that does not scale, such as "center".

Try this out:

<layer-list>
    <item>
        <shape>
            <stroke android:width="0.9dp" android:color="@color/colorPrimaryDark" />
            <solid android:color="#FFF" />
            <corners android:radius="16dp" />
        </shape>
    </item>

    <item android:left="18dp">
        <bitmap
            android:src="@drawable/arrow_drawable"
            android:gravity="center_vertical|left"/>
    </item>
</layer-list>

推荐阅读