Android 自定义分级条着色API<;21带有支持库22.1.1

Android 自定义分级条着色API<;21带有支持库22.1.1,android,android-support-library,android-drawable,Android,Android Support Library,Android Drawable,我正在使用支持库22.1.1。我的目标是为自定义分级栏着色,以避免在我的APK中包含多个图像 API 22上的着色正确,但API 19上的着色不正确。我希望它能在API>=16上工作。请注意,我只是尝试将“进度”着色,而不是将整个条着色 以下是分级栏样式: <style name="customRatingBar" parent="Widget.AppCompat.RatingBar"> <item name="android:progressDrawable">

我正在使用支持库22.1.1。我的目标是为自定义分级栏着色,以避免在我的APK中包含多个图像

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

以下是分级栏样式:

<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>
在我的布局中就是这样应用的,在扩展安卓.support.v4.fragment的片段中,在扩展安卓.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
。有没有办法不使用几个绘图工具就可以实现这一点?

使用support library>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之前不起作用,因此我认为这将解决您的问题。

我不想编辑Gary已被接受的答案(如果我可以的话),但我发现完整/部分/无的顺序对我不正确。这就是我实现它的方式,它适用于API 18+

要将starbar(我的AppCompatingBar子类)设置为黄色,将未填充的星号设置为灰色,请执行以下操作:

    int yellow = ContextCompat.getColor(getContext(), R.color.starbar_star_yellow);
    int gray = ContextCompat.getColor(getContext(), R.color.starbar_star_gray);

    Drawable starbarDrawable = this.getProgressDrawable();
    DrawableCompat.setTint(starbarDrawable, yellow); // this works for API 21+

    // this ensures it works for API 18-20
    if(starbarDrawable instanceof LayerDrawable) {
        LayerDrawable stars = (LayerDrawable) starbarDrawable;
        stars.getDrawable(0).setColorFilter(gray, PorterDuff.Mode.SRC_ATOP); // UNFILLED
        stars.getDrawable(1).setColorFilter(yellow, PorterDuff.Mode.SRC_ATOP); // PARTIAL OR FULL?
        stars.getDrawable(2).setColorFilter(yellow, PorterDuff.Mode.SRC_ATOP); // PARTIAL OR FULL?
    }

在4.1.1上,getProgressDrawable返回的drawable不强制转换为LayerDrawable,并提供了一个强制转换例外Tanks Gary,这帮助我获得了下面发布的最终解决方案。
    int yellow = ContextCompat.getColor(getContext(), R.color.starbar_star_yellow);
    int gray = ContextCompat.getColor(getContext(), R.color.starbar_star_gray);

    Drawable starbarDrawable = this.getProgressDrawable();
    DrawableCompat.setTint(starbarDrawable, yellow); // this works for API 21+

    // this ensures it works for API 18-20
    if(starbarDrawable instanceof LayerDrawable) {
        LayerDrawable stars = (LayerDrawable) starbarDrawable;
        stars.getDrawable(0).setColorFilter(gray, PorterDuff.Mode.SRC_ATOP); // UNFILLED
        stars.getDrawable(1).setColorFilter(yellow, PorterDuff.Mode.SRC_ATOP); // PARTIAL OR FULL?
        stars.getDrawable(2).setColorFilter(yellow, PorterDuff.Mode.SRC_ATOP); // PARTIAL OR FULL?
    }