gpt4 book ai didi

java - 如何在低于 21 的 Android API 中使用 VectorDrawables?

转载 作者:IT老高 更新时间:2023-10-28 13:23:35 24 4
gpt4 key购买 nike

我正在开发一个 Android 项目,我选择了 <vector>显示图标,因为它具有适应性和动态性,但是,我只能在运行 Android 的设备上运行此应用程序,这些设备具有 API 21 或更高版本。我的问题是如何使用 <vector>在较低的 Android 版本上,即 API 14 或类似的。谢谢!

<!-- drawable/ic_android_debug_bridge.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="48dp"
android:width="48dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="@color/primaryColorDark"
android:pathData="M15,9A1,1 0 0,1 14,8A1,1 0 0,1 15,7A1,1 0 0,1 16,8A1,1 `0 0,1 15,9M9,9A1,1 0 0,1 8,8A1,1 0 0,1 9,7A1,1 0 0,1 10,8A1,1 0 0,1 9,9M16.12,4.37L18.22,2.27L17.4,1.44L15.09,3.75C14.16,3.28 13.11,3 12,3C10.88,3 9.84,3.28 8.91,3.75L6.6,1.44L5.78,2.27L7.88,4.37C6.14,5.64 5,7.68 5,10V11H19V10C19,7.68 17.86,5.64 16.12,4.37M5,16C5,19.86 8.13,23 12,23A7,7 0 0,0 19,16V12H5V16Z" /></vector>

最佳答案

使用支持库 23.2,一直到 API v7 都提供了对 Vector Drawables 的真正支持。建议禁用以前版本的支持,它在构建时呈现 PNG,通过添加

// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}

build.gradle 文件。

实现相当简单。只需在 Drawables 上使用新的 srcCompat 属性(在应用命名空间下!):

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_add" /> // <= this is new

在 TextView 的 drawableLeft 属性等情况下也支持 vector 可绘制对象。

来源:library announcement

但是,我仍然会推荐 Iconics library 之类的内容。 , AndroidSVG ,或其他字体图标或 SVG 解决方案,以提供完整的 SVG 标准 vector 支持。

关于java - 如何在低于 21 的 Android API 中使用 VectorDrawables?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34417843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com