Android modifies the color of native RatingBar

First, depend on your build.gradle

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

Then let your activity inherit Android.support.v7.app.AppCompatActivity

public class MainActivity extends AppCompatActivity {  
    ...
}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

Declare the following attributes in style.xml

 <style name="RatingBar" parent="Theme.AppCompat">
        <item name="colorControlNormal">@color/blue</item>
        <item name="colorControlActivated">@color/red_warn</item>
    </style>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

ColorControl Normal represents the color when unchecked, and colorControl Activated represents the color when unchecked.
Then write the following code in the ratingbar control in activity xml

 <android.support.v7.widget.AppCompatRatingBar
            android:id="@+id/rating_bar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="?attr/ratingBarStyleIndicator"
            android:isIndicator="false"
            android:theme="@style/RatingBar"
            />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

In this way, with the ratingbar system, there will be no problem that some models and some stars can not be blocked out, and the color of ratingbar can be set by itself.

**

To update

* *! The second way! The following code can also modify the color of ratingbar

<android.support.v7.widget.AppCompatRatingBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rb"
        android:stepSize="1"
        android:rating="5"
        style="?attr/ratingBarStyle"
        android:layout_marginTop="10dip"
        android:progressTint="@color/orange_main_color"
        android:progressBackgroundTint="@color/app_main_gray_bg"
        android:secondaryProgressTint="@color/app_main_gray_bg"
       />
How to adjust ratingbar Size of the Star

Laboratory introduction zwatch One of the bigger problems in development is how to develop 240.*240 Small screen for screen adaptation. Today's sleep quality rating in sleep tracking has introduced a special favorite ratingbar,Result rating star Too big, leading to stars crossing the border, the last star directly beyond the screen. There are roughly three methods searched online, only one is more in line with the appetite, here to make a simple summary.

I. Adjustment of Attribute Value

  1. <RatingBar  
  2.                         android:id="@+id/ratingBar1"  
  3.                         android:layout_width="wrap_content"  
  4.                         android:layout_height="wrap_content"  
  5.                         android:layout_gravity="center_horizontal"  
  6.                         android:isIndicator="true"  
  7.                         <strong><span style="color:#ff0000;">android:maxWidth="30dp"  
  8.                      android:maxHeight="30dp"</span></strong>  
  9.                         android:soundEffectsEnabled="true" />  

It's supposed to be the easiest way to think about it. Unfortunately, Android development engineers haven't considered these two attributes as carefully as we expected. These two attributes are not the same as those of most controls. They just mean the overall size of ratingbar. The pity is that if ratingbar becomes smaller as a whole, star would rather lose than enlarge or shrink with the whole. This point I think the new version of platform should conform to the public opinion! Unnatural attributes.

II. style Adjustment

The only difference between the code and the one above is to add a line of attributes:

style="?android:attr/ratingBarStyleSmall" 
After doing this, the stars really get smaller! The moment I saw the stars becoming smaller, my tmd tears came out. That's too small!! ____________
I can't stand it. It's the size of the pen tip when it comes to the mobile phone.
3. Custom style
Although it took one night to complete the experiment, I still felt that I had learned something. Before I tried the third method, I read various posts asking how to call the cloud directly without customizing it. Unfortunately, we have been looking for the style of the lowest platform s and found that Android only provides Style Small and Style and an Indicator.
style="?android:attr/ratingBarStyleSmall"
style="?android:attr/ratingBarStyle"
style="?android:attr/ratingBarStyleIndicator"
Neither size is satisfactory. The last decision is to use the most troublesome method.

Next, I'll talk about how to make my own RatingBar style.  
Define a style in the res/data/style.xml file:
  1. <resources>  
  2. <style name="myratingbar"   
  3. parent="@android:style/Widget.RatingBar.Small">  
  4.   
  5.   
  6. <item   
  7. name="android:progressDrawable">  
  8. @drawable/myratingbar  
  9. </item>  
  10. <item name="android:minHeight">36dip</item>  
  11. <item name="android:maxHeight">36dip</item>  
  12. <style>  
  13.   
  14.   
  15. </resources>  
The inherited parent class can choose RatingBar, RatingBar.Indicator,RatingBar.Small;
Size can be defined by itself.  
Note: The differences between RatingBar, RatingBar. Indicator and RatingBar. Small are that besides their different display sizes, the IsIndicator attribute in RatingBar is false, and the other two are true. This attribute is false to indicate the click events that can be used accordingly, by which users can interact with each other.
2. Specify image resources for different layers in res/drawable/myratingbar.xml file
  1. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
  2.   
  3. <item android:id="@+android:id/background"   
  4. android:drawable="@drawable/myratingbar_off" />  
  5.   
  6. <item android:id="@+android:id/secondaryProgress"  
  7. android:drawable="@drawable/myratingbar_half" />  
  8.   
  9. <item android:id="@+android:id/progress"   
  10. android:drawable="@drawable/myratingbar_on" />  
  11.   
  12. </layer-list>   
The third method mentioned above originates from the Internet and is not original. Thank the original author for providing it. After setting up the above two files, you can set @style/myratingbar in the xml layout file.
Another small detail: be sure to note that style is written as layer-list, just like the above code, otherwise the system will not recognize it! I forgot about the mistake in the newspaper.

Self definition RatingBar Style:

xml File code

<RatingBar
    android:id="@+id/ratingBar1"
    style="@style/myRatingBar"
    android:layout_width="wrap_content"
    android:layout_height="33dp"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:numStars="5"
    android:rating="5"
    android:stepSize="1.0" />

Some attribute meanings:

numStars: Number of Stars

rating: Select several stars by default

stepSize: How much less / more each time

The main points are:

 style="@style/myRatingBar"
Custom style:

<style name="myRatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/em_ratingbar_drawable</item>
    <item name="android:minHeight">50dp</item>
    <item name="android:maxHeight">50dp</item>
</style>
The main points are:

 <item name="android:progressDrawable">@drawable/em_ratingbar_drawable</item>

Cut the picture of the stars by oneself, and get out how big it is, that is how big it is.

test No, minHeight and Max Herght.

em_ratingbar_drawable in drawable

<?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="@mipmap/em_icon_star_normal">
    </item>
    <item
        android:id="@android:id/secondaryProgress"
        android:drawable="@mipmap/em_icon_star_normal">
    </item>
    <item
        android:id="@android:id/progress"
        android:drawable="@mipmap/em_icon_star_select">
    </item>

</layer-list>
Note: Must be layer_list




   

    

Keywords: Android xml Attribute Gradle

Added by jim_de_bo on Wed, 29 May 2019 21:21:24 +0300