【发布时间】:2020-07-01 14:19:24
【问题描述】:
以下 xml 代码用于在 API 23+ 的 layer-list 的项目内设置图像可绘制对象的宽度和高度:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item><shape android:shape="rectangle"><solid android:color="@color/white"/></shape></item>
<item android:drawable="@drawable/my_drawable" android:gravity="center" android:bottom="100dp"
android:width="100dp" android:height="100dp" />
<item android:drawable="@drawable/drawable_text" android:gravity="center" android:top="50dp"
android:width="200dp" android:height="200dp" />
</layer-list>
但是,<item> 中的 android:width 和 android:height 仅适用于 23 及以上的 API 级别。如果没有明确的宽度和高度值,drawable 将溢出屏幕,如果我使用调整大小的 PNG 图像,质量会变差。
我搜索了有关 Stackoverflow 的解决方法(here、here 和 here),但似乎没有一个解决方案有效。
- 添加
android:top、android:bottom、android:left和android:right不要调整/设置drawable的高度和宽度,可能是因为它们是PNG 图像而不是矢量绘图。 - 将
drawable添加到shape(然后使用size标签)不起作用。
【问题讨论】:
标签: android xml layer-list