2016-08-23 17 views
2

に関する問題[EDIT 3] - ワーキング今すぐ @sJyの提案に、以下のようにコードを変更:RatingBar - 評価に基づいて変更星の色 - ロリポップ

LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable(); 

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
         DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar)); // Partial star 
         DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar)); 
         DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(0)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorPrimary)); 
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
         layerDrawable.getDrawable(1).setTint(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar)); 
         layerDrawable.getDrawable(2).setTint(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar)); 
         layerDrawable.getDrawable(0).setTint(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorPrimary)); 
        } else { 
         layerDrawable.getDrawable(2).setColorFilter(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar), PorterDuff.Mode.SRC_ATOP); 
        } 

enter image description here


をI AppCompatRatingBarを使用してユーザーが選択した評価を表示しています。このコードはKitKatとMarshmallowではうまく動作しますが、Lollipopでは星の色は変わりません。

イメージと以下のコードを参照してください: - 4つ星

enter image description here

ロリポップ -

キットカットを3つ星

- 2つ星

enter image description here

マシュマロをクリック

enter image description here

コード:

ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { 
      @Override 
      public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { 


       if ((int) rating == 1) { 

        ratingBar.setNumStars(5); 
        ratingBar.setProgress(1); 
        ratingBar.setRating(1); 

        LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable(); 

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
         DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar)); 
         DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar)); 

        }else { 
         layerDrawable.getDrawable(2).setColorFilter(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar), PorterDuff.Mode.SRC_ATOP); 
        } 

        textViewRating.setText(getResources().getString(R.string.worst)); 

       } else if ((int) rating == 2) { 

        ratingBar.setNumStars(5); 
        ratingBar.setProgress(2); 
        ratingBar.setRating(2); 

        LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable(); 

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
         DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorTwoStars)); 
         DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorTwoStars)); 

        }else { 
         layerDrawable.getDrawable(2).setColorFilter(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorTwoStars), PorterDuff.Mode.SRC_ATOP); 
        } 

        textViewRating.setText(getResources().getString(R.string.poor)); 
       } else if ((int) rating == 3) { 

        ratingBar.setNumStars(5); 
        ratingBar.setProgress(3); 
        ratingBar.setRating(3); 

        LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable(); 

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
         DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorThreeStars)); 
         DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorThreeStars)); 
        }else { 
         layerDrawable.getDrawable(2).setColorFilter(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorThreeStars), PorterDuff.Mode.SRC_ATOP); 
        } 

        textViewRating.setText(Html.fromHtml(getResources().getString(R.string.onetime_watch_one_line))); 
       } else if ((int) rating == 4) { 

        ratingBar.setNumStars(5); 
        ratingBar.setProgress(4); 
        ratingBar.setRating(4); 

        LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable(); 

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
         DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorFourStars)); 
         DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorFourStars)); 
        }else { 
         layerDrawable.getDrawable(2).setColorFilter(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorFourStars), PorterDuff.Mode.SRC_ATOP); 
        } 

        textViewRating.setText(getResources().getString(R.string.good)); 
       } else if ((int) rating == 5) { 

        ratingBar.setNumStars(5); 
        ratingBar.setProgress(5); 
        ratingBar.setRating(5); 

        LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable(); 

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
         DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorFiveStars)); 
         DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorFiveStars)); 
        }else { 
         layerDrawable.getDrawable(2).setColorFilter(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorFiveStars), PorterDuff.Mode.SRC_ATOP); 
        } 

        textViewRating.setText(getResources().getString(R.string.excellent)); 
       } 

      } 

     }); 

[EDIT 1] Sohailの提案@を1としてRatingBarをクリックしたとき、私は

DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(0)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorPrimary)); 

を追加し、それが消えます。明確にするために、私はRatingBarに背景色を追加しました。

まだ

enter image description here

解決法はありません後

​​

!それは場合に役立ちますAppCompatRatingBar XMLを追加

[EDIT 2] は、:

<android.support.v7.widget.AppCompatRatingBar 
        android:id="@+id/ratingBar" 
        style="?android:attr/ratingBarStyle" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:layout_marginBottom="8dp" 
        android:background="@color/colorFiveStars" 
        android:isIndicator="false" 
        android:max="5" 
        android:numStars="5" 
        android:stepSize="1" /> 

は、任意の変更によってスタイル、問題の原因ですか?

+0

を使用あなたはRatingBar

final ColorStateList colorStateList = ColorStateList.valueOf(<int color value>) 

に使用する色を使用してColorStateを作成します。 '。これを参照してください:http://stackoverflow.com/questions/39060888/autocompletetextview-strange-behaviour-in-ollipop-device – Ironman

+1

@アイロンマン、それは.. –

+0

あなたが使用しているライブラリのバージョンをサポートしていますか? – sJy

答えて

1

は直接setTint()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
    layerDrawable.getDrawable(1).setTint(getResources().getColor(R.color.colorOneStar)); 
    layerDrawable.getDrawable(2).setTint(getResources().getColor(R.color.colorOneStar)); 
} 
を使用して色合いを設定してみてください

注:

+0

私は色合いを直接設定するときに働いています。ありがとうございました!!! –

1

すべての状態を設定するEmpty starHalf starFull star評価が変更されました。

LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable(); 
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(0)), Color.WHITE); // Empty star 
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), Color.parseColor("#757675")); // Partial st 
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), Color.parseColor("#757675")); // Full 

更新作業のデモ:

MainActivity

package pk.sohail.gallerytest.activity; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Color; 
import android.graphics.drawable.LayerDrawable; 
import android.os.Bundle; 
import android.support.v4.graphics.drawable.DrawableCompat; 
import android.widget.RatingBar; 

import pk.sohail.gallerytest.R; 

public class MainActivity extends Activity { 

    Context context; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     context = this; 
     RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar); 
     ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { 
      @Override 
      public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { 

       LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable(); 
       DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(0)), Color.parseColor("#3C3F41")); 
       DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), Color.parseColor("#F7C065")); 
       DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), Color.parseColor("#F7C065")); 
      } 
     }); 
    } 
} 

activity_main。XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@android:color/white"> 


    <android.support.v7.widget.AppCompatRatingBar 
     android:id="@+id/ratingBar" 
     style="?android:attr/ratingBarStyle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:layout_gravity="center_horizontal" 
     android:isIndicator="false" 
     android:numStars="5" 
     android:stepSize="1" /> 
</RelativeLayout> 

のアプリのGradle:

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.3" 

    defaultConfig { 
     applicationId "pk.sohail.gallerytest" 
     minSdkVersion 19 
     targetSdkVersion 23 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:appcompat-v7:23.0.1' 

} 

enter image description hereenter image description here enter image description hereenter image description here

+0

更新された質問を参照してください。 –

+0

更新された回答を確認してください –

+0

私はあなたと同じコードを持っていますが、何らかの理由で動作しません!私はv7を使用しています:24.1.1サポートlib、あなたのものは何ですか? –

0

奇妙なロリポップこの問題を持っている(、マシュマロをロリポップでテスト済み)GETCOLOR(INT)が廃止されましたが、それはまだ動作しますが、なぜわかりません! これを使ってこの問題を取り除きました。それはLolipopとLolipopの前後のすべてのLolipopで動作します。

その後、私は、私はロリポップ `に問題がなければなりませんがあると思います` AutoCompleteTextView`と同じ問題を抱えているViewCompat

ViewCompat.setBackgroundTintList(<Your RatingBar>,colorStateList) 
関連する問題