android - 自定义 RatingBar 着色 API < 21,支持库 22.1.1

标签 android android-support-library android-drawable

我正在使用支持库 22.1.1。我的目标是为自定义 RatingBar 着色,以避免在我的 APK 中包含多张图片。

着色在 API 22 上正确应用,但在 API 19 上不正确。我希望它在 API >= 16 上工作。请注意,我只是尝试对“进度”进行着色,而不是对整个栏进行着色。

这是 RatingBar 样式:

<style name="customRatingBar" parent="Widget.AppCompat.RatingBar">
    <item name="android:progressDrawable">@drawable/custom_ratingbar</item>
    <item name="android:minHeight">24dp</item>
    <item name="android:maxHeight">24dp</item>
    <item name="android:progressTint">@color/md_red_700</item>
    <item name="android:secondaryProgressTint">@color/md_red_700</item>
</style>

custom_ratingbar.xml 文件:

<?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/ic_heart_outline_grey600_24dp" />
    <item android:id="@android:id/progress"
        android:drawable="@drawable/ic_heart_grey600_24dp" />
</layer-list>

它在我的布局中以这种方式应用,它在扩展 android.support.v4.Fragment 本身的 fragment 中,在扩展 android.support.v7.app.AppCompatActivity:

<RatingBar
    android:layout_width="wrap_content"
    android:layout_height="22dp"
    android:id="@+id/checkin_rating"
    style="@style/customRatingBar"
    android:isIndicator="true"
    android:numStars="5"
    android:stepSize="1"
    android:layout_marginBottom="8dp"/>

我从 lint 工具收到一条警告,说 API < 21 不支持 android:progressTint。有没有办法在不使用多个可绘制对象的情况下实现这一点?

最佳答案

借助支持库 > 22 我设法使用 getProgressDrawable

做到了这一点
LayerDrawable stars = (LayerDrawable) bar.getProgressDrawable();
stars.getDrawable(0).setColorFilter(NONE_COLOR, PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(1).setColorFilter(FULL_COLOR, PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(2).setColorFilter(PARTIAL_COLOR, PorterDuff.Mode.SRC_ATOP);

这在支持库 22 之前不起作用,所以我认为这会解决您的问题。

关于android - 自定义 RatingBar 着色 API < 21,支持库 22.1.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30413726/

相关文章:

android - 是否可以以编程方式设置 drawableLeft 可见性

java.lang.IllegalStateException : Couldn't create Engine

android - onListItemClick 不适用于自定义 ListView

android - DragEvent 的 getX() 和 getY() 方法返回的数字究竟是多少?

android - Android Studio 中的 "package android.support.v7.app does not exist"错误

android - Android drawable的clip-path层可以旋转吗?

android - 设备所有者 QR 停止在 Android12 设备上工作,获取无法设置设备消息

java - 如何修复错误包 android.support.v7 不存在?

android - ViewPager.setOffscreenPageLimit(0) 无法按预期工作

android - TabHost TabSpec setIndicator 中的标签和图标是否相互排斥?