Android RatingBar矢量图标没有显示

Dav*_*vid 7 android android-layout ratingbar

我给出了我想为用户显示RatingBar的布局.我使用自己的自定义图标而不是默认星标.问题是图标是矢量格式,当我使用它时我得到奇怪的效果(见下图)它应该显示5个图标,但它只显示一个为什么是这样? 在此输入图像描述

当我将矢量图像转换为png时,一切都很好 在此输入图像描述

我将在这里添加我的代码我的图标代码(在drawable文件夹中)//empty_rate.xml

//middle_rate.xml
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:viewportWidth="48"
    android:viewportHeight="48"
    android:width="4dp"
    android:height="4dp">
    <group
        android:translateX="117"
        android:translateY="-118.5">
        <path
            android:pathData="M-93 122.5c-11 0 -20 9 -20 20 0 11 9 20 20 20 11 0 20 -9 20 -20 0 -11.1 -9 -20 -20 -20zm0 36c-8.8 0 -16 -7.2 -16 -16 0 -8.8 7.2 -16 16 -16 8.8 0 16 7.2 16 16 0 8.8 -7.2 16 -16 16z"
            android:fillColor="#076127" />
        <path
            android:pathData="M-105.2 142.5c0 -6.7 5.4 -12.2 12.2 -12.2l0 24.3c-6.7 0 -12.2 -5.4 -12.2 -12.1z"
            android:fillColor="#076127" />
    </group>
</vector>

//full_rate.xml
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:viewportWidth="48"
    android:viewportHeight="48"
    android:width="8dp"
    android:height="8dp">
    <group
        android:translateX="117"
        android:translateY="-118.5">
        <path
            android:pathData="M-93 122.5c-11 0 -20 9 -20 20 0 11 9 20 20 20 11 0 20 -9 20 -20 0 -11.1 -9 -20 -20 -20zm0 36c-8.8 0 -16 -7.2 -16 -16 0 -8.8 7.2 -16 16 -16 8.8 0 16 7.2 16 16 0 8.8 -7.2 16 -16 16z"
            android:fillColor="#076127" />
        <path
            android:pathData="M-80.8 142.5a12.2 12.2 0 0 1 -12.2 12.2 12.2 12.2 0 0 1 -12.2 -12.2 12.2 12.2 0 0 1 12.2 -12.2 12.2 12.2 0 0 1 12.2 12.2z"
            android:fillColor="#076127" />
    </group>
</vector>
Run Code Online (Sandbox Code Playgroud)

这是我的布局评级栏

<RatingBar
                android:isIndicator="true"
                style="@style/CustomRatingBar"
                android:layout_gravity="right"
                android:id="@+id/rate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:numStars="5"/>
Run Code Online (Sandbox Code Playgroud)

这是我的styles.xml

<style name="CustomRatingBar" parent="@android:style/Widget.RatingBar">
        <item name="android:progressDrawable">@drawable/custom_ratingbar_selector</item>
        <item name="android:minHeight">24dip</item>
        <item name="android:maxHeight">24dip</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

这是我的custom_ratingbar_selector.xml(drawable文件夹)

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background"
        android:drawable="@drawable/empty_rate" />
    <item android:id="@android:id/secondaryProgress"
        android:drawable="@drawable/middle_rate" />
    <item android:id="@android:id/progress"
        android:drawable="@drawable/full_rate" />
</layer-list>
Run Code Online (Sandbox Code Playgroud)

那有什么不对?如何解决问题?

DEX*_*7RA -1

矢量绘图向后兼容解决方案

\n

在运行低于 Android 5.0(API 级别 21)的平台版本的设备上支持矢量绘图和动画矢量绘图,VectorDrawableCompat分别AnimatedVectorDrawableCompat通过两个支持库提供:support-vector-drawableanimated-vector-drawable

\n

Android Studio 1.4通过在构建时生成 PNG 文件引入了对矢量绘图的有限兼容性支持。然而,矢量绘图和动画矢量绘图支持库提供了灵活性和广泛的兼容性 \xe2\x80\x94 它是一个支持库,因此您可以将它与所有 Android 平台版本一起使用,直到 Android 2.1(API 级别 7+)。要将您的应用程序配置为使用矢量支持库,请将该元素添加到应用程序模块中的文件中。vectorDrawablesbuild.gradle

\n

使用以下代码片段来配置vectorDrawables元素:

\n

对于 Gradle 插件 2.0+:

\n
android {\n   defaultConfig {\n     vectorDrawables.useSupportLibrary = true\n    }\n }\n
Run Code Online (Sandbox Code Playgroud)\n

对于 Gradle 插件 1.5 或更低版本:

\n
android {\n  defaultConfig {\n    // Stops the Gradle plugin\xe2\x80\x99s automatic rasterization of vectors\n    generatedDensities = []\n  }\n  // Flag notifies aapt to keep the attribute IDs around\n  aaptOptions {\n    additionalParameters "--no-version-vectors"\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

更新

\n

我认为您没有使用低于 21 的 API 级别(因为它现在已修复),但如果有人仍然面临该解决方案,因为他需要较低的 API,那么您就可以了。\n即使问题是,这也是正确的技术解决方案已弃用。干杯!

\n