android - 图层列表内的可绘制矢量正在拉伸
问题描述
当我使用内部带有矢量可绘制对象的图层列表时,它在 API 23+ 上运行良好,但不是之前的。
我已启用vectorDrawables.useSupportLibrary = true
and AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
,但可绘制对象非常拉伸。
左边是奥利奥 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 中的预览显示的内容
解决方案
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>
推荐阅读
- java - 使用 Spring Boot 将具有基本身份验证的自动登录客户端自动登录到其他网站
- python - 熊猫将元素放置在错误的位置
- android - 如何使不同的视图 RecyclerView 成为无限循环?
- r - 基于组计算存在/不存在
- php - Symfony DomCrawler 如何在某些元素之后插入 html?
- java - 多个子recyclerview和listview
- android - 让 Android sdkmanager 与 Java 11 一起运行
- c# - 从下拉列表中捕获一个值,其中显示下拉列表不是默认值
- c# - ARCore Unity - 被跟踪对象(视频)未正确缩放到图像目标
- awk - 获取属性的文本处理