【问题标题】:Android RatingBar vector icon is not showingAndroid RatingBar 矢量图标未显示
【发布时间】:2016-10-08 09:42:41
【问题描述】:

我有一个layout,我想在其中为用户显示RatingBar。我正在使用我自己的自定义图标而不是默认星星。我的问题是图标是矢量格式,当我使用它们时,我会得到奇怪的效果(见下图)。它应该显示5个图标,但它只显示一个,这是为什么呢?

当我将矢量图像转换为.png 时,一切都很好。

我将在这里添加我的代码 我的图标代码(在drawable 文件夹中)//empty_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" />
    </group>
</vector>

//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>

这是我的layout RatingBar

<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"/>

这是我的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>

这是我的custom_ratingbar_selector.xmldrawable 文件夹)

<?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>

那怎么了?如何解决问题?

【问题讨论】:

标签: android android-layout ratingbar


【解决方案1】:

Vector drawables 向后兼容解决方案

为了在运行平台版本低于 Android 5.0(API 级别 21)的设备上支持矢量可绘制和动画矢量绘制,VectorDrawableCompatAnimatedVectorDrawableCompat 可通过两个支持库分别获得:support-vector-drawableanimated-vector-drawable

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

使用以下代码sn-p配置vectorDrawables元素:

对于 Gradle 插件 2.0+:

android {
   defaultConfig {
     vectorDrawables.useSupportLibrary = true
    }
 }

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

android {
  defaultConfig {
    // Stops the Gradle plugin’s automatic rasterization of vectors
    generatedDensities = []
  }
  // Flag notifies aapt to keep the attribute IDs around
  aaptOptions {
    additionalParameters "--no-version-vectors"
  }
}

更新

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

【讨论】:

    猜你喜欢
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    相关资源
    最近更新 更多