Android RatingBar change star colors [closed]

2019-01-03 04:07发布

How can i change the star colors and how can i change the size of the stars ?

30条回答
Rolldiameter
2楼-- · 2019-01-03 04:48

The solutions that Alex and CommonsWares have posted are correct. One thing that the Android never talks about though is proper pixel sizes for different densities. Here are the required dimensions for each density based on halo light.

Small Star

mdpi: 16px
hdpi: 24px
xhdpi: 32px
xxhdpi: 48px

Medium Star

mdpi: 24px
hdpi: 36px
xhdpi: 48px
xxhdpi: 72px

Large Star

mdpi: 35px
hdpi: 52px
xhdpi: 69px
xxhdpi: 105px
查看更多
迷人小祖宗
3楼-- · 2019-01-03 04:48

I found a simple solution for changing the color of the star according to your theme.

Goto this site : http://android-holo-colors.com/

Choose your theme color and get your star images created.

查看更多
SAY GOODBYE
4楼-- · 2019-01-03 04:50

Building @lgvalle's answer.

2015 Update

Now you can use DrawableCompat to tint all kind of drawables. For example:

Drawable progress = ratingBar.getProgressDrawable(); DrawableCompat.setTint(progress, Color.WHITE); This is backwards compatible up to API 4

LayerDrawable drawable = (LayerDrawable) getProgressDrawable();
Drawable progress = drawable.getDrawable(2);
DrawableCompat.setTint(progress, getResources().getColor(COLOR1));
progress = drawable.getDrawable(1);
DrawableCompat.setTintMode(progress, PorterDuff.Mode.DST_ATOP);
DrawableCompat.setTint(progress, getResources().getColor(COLOR1));
DrawableCompat.setTintMode(progress, PorterDuff.Mode.SRC_ATOP);
DrawableCompat.setTint(progress, getResources().getColor(COLOR2));
progress = drawable.getDrawable(0);
DrawableCompat.setTint(progress, getResources().getColor(COLOR2));

This will keep the fraction steps colors.

查看更多
SAY GOODBYE
5楼-- · 2019-01-03 04:51

From the API 21 on it's very easy to change the color of the stars with this three lines:

android:progressTint="@android:color/holo_red_dark"
android:progressBackgroundTint="@android:color/holo_red_dark"
android:secondaryProgressTint="@android:color/holo_red_dark" 

Doing so, you change the background color and the border color of the stars.

查看更多
SAY GOODBYE
6楼-- · 2019-01-03 04:52

To change the color you just have to put set the parameter android:progressTint

 <RatingBar
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginTop="15dp"
        android:numStars="5"
        android:rating="1"
        android:progressTint="@android:/color/black"
        android:layout_gravity="center"
       />

For the size the style property.

查看更多
唯我独甜
7楼-- · 2019-01-03 04:53

The easiest way that worked for me...if you are extending AppCompat Activity

In your build.gradle add latest appcompat library.

dependencies {  
    compile 'com.android.support:appcompat-v7:X.X.X' // where X.X.X version
}

Make your activity extend android.support.v7.app.AppCompatActivity

public class MainActivity extends AppCompatActivity {  
    ...
}

Declare custom style in your styles.xml file.

<style name="RatingBar" parent="Theme.AppCompat">  
    <item name="colorControlNormal">@color/indigo</item>
    <item name="colorControlActivated">@color/pink</item>
</style>  

Apply this style to your RatingBar via android:theme attribute.

<RatingBar  
    android:theme="@style/RatingBar"
    android:rating="3"
    android:stepSize="0.5"
    android:numStars="5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
查看更多
登录 后发表回答