Android的RatingBar实现拖动评星

时间:2022-03-23 05:44:57

自定义RatingBar星星图片(res/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="@drawable/start1"></item>               
                <item android:id="@+android:id/secondaryProgress"
                android:drawable="@drawable/start3"></item>
                <item android:id="@+android:id/progress"
        android:drawable="@drawable/start2"></item>
</layer-list>

background:背景图片

secondaryProgress:当android:stepSize="0.5"不等于整数的时候,出现暂时的第二层背景图片

progress:类似进度条,点亮的星星背景图片

定义RatingBar调用的style(style.xml)

<style name="roomRatingBar" parent="@android:style/Widget.RatingBar">
        <item name="android:progressDrawable">@drawable/ratbar_background1</item>
        <item name="android:minHeight">50dp</item>
        <item name="android:maxHeight">50dp</item>
    </style>

android:minHeight:设置星星图片的尺寸

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
     <!-- 星级评分条 -->
    <TextView  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="第一种风格(可交互)" />  
    <RatingBar   
        android:id="@+id/ratingbar"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        style="@style/roomRatingBar"
        android:numStars="5"  
        android:stepSize="0.5"
        android:rating="3.0"/>  
     <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="第二种风格(ratingBarStyleSmall)" />  
    <RatingBar   
        android:id="@+id/ratingbar2"  
        style="?android:attr/ratingBarStyleSmall"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:numStars="5" 
        android:rating="3.0" 
        android:stepSize="1"/>  
      
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="第三种风格(ratingBarStyleIndicator)" />  
    <RatingBar   
        android:id="@+id/ratingbar3"  
        style="?android:attr/ratingBarStyleIndicator"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:numStars="5" 
        android:rating="3.0" 
        android:stepSize="1"/>  
    
</LinearLayout>


Activity文件:

package com.app.fengactivity;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.RatingBar;


public class MainActivity extends Activity {
 private RatingBar ratingbar=null;//星级评分条
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
       ratingbar = (RatingBar)findViewById(R.id.ratingbar); 
        
        ratingbar.setOnRatingBarChangeListener(new RatingBarListener());
}




private class RatingBarListener implements RatingBar.OnRatingBarChangeListener{  

    public void onRatingChanged(RatingBar ratingBar, float rating,  
            boolean fromUser) {  
        System.out.println("等级:" + rating);  
        System.out.println("星星:" + ratingBar.getNumStars());  
    }  
}  
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}


截图:、

Android的RatingBar实现拖动评星