【问题标题】:How to set drawable (PNG) width and height in layer-list for API level below 23?如何为低于 23 的 API 级别在图层列表中设置可绘制 (PNG) 宽度和高度?
【发布时间】: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>

但是,&lt;item&gt; 中的 android:widthandroid:height 仅适用于 23 及以上的 API 级别。如果没有明确的宽度和高度值,drawable 将溢出屏幕,如果我使用调整大小的 PNG 图像,质量会变差。

我搜索了有关 Stackoverflow 的解决方法(hereherehere),但似乎没有一个解决方案有效。

  1. 添加android:topandroid:bottomandroid:leftandroid:right 不要调整/设置drawable 的高度和宽度,可能是因为它们是PNG 图像而不是矢量绘图。
  2. drawable 添加到shape(然后使用size 标签)不起作用。

【问题讨论】:

    标签: android xml layer-list


    【解决方案1】:

    没有办法实现你的目标。您必须调整图像大小。或者,您可以使用 android:widthandroid:height 用于 API 级别 >=23,并使用 layer-list 调整大小的图像用于 API 级别

    此外,您不能使用android:topandroid:bottom 等属性来调整任何图像的大小(不是 png 或矢量可绘制),这些属性用于定位。

    【讨论】:

    • 你好 Garnik,是的,我已经这样做了:我使用 android:width 和 android:height API 级别 23+,低于 23 我使用调整大小的图像。感谢您的确认。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 2017-01-17
    • 2013-05-04
    • 2017-03-16
    • 2021-03-02
    • 2016-04-10
    相关资源
    最近更新 更多