如果要想自己实现评分的显示图片,那么就必须准备两张图片,一张为填充色的,一张为空色的.
而在Android之中现在根本就不知道有这样的两张图片需要替换掉评分的默认图片,那么就必须
写一个配置文件,找到这两个文件。
这个文件可以在res/drawable-hdpi下编写一个图片文件——star_conf_file.xml
当此配置文件完成之后,实际上这个文件保存在图片文件夹之中,那么就需要在values下编写一个专门的
样式表文件styles.xml
在main.xml中
<?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" >
<RatingBar
android:id="@+id/ratingbar"
android:numStars="5"
android:stepSize="1"
android:isIndicator="false"
style="@style/myRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="40dp"/>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="40dp"/>
</LinearLayout>
在res/drawable-hdpi新建star_conf_file.xml
<?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/star_empty" />
<item
android:id="@+android:id/secondaryProgress"
android:drawable="@drawable/star_empty" />
<item
android:id="@+android:id/progress"
android:drawable="@drawable/star_full" />
</layer-list>
在values下新建styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="myRatingBar" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">
@drawable/star_conf_file
</item>
<item name="android:minHeight">53dp</item>
<item name="android:maxHeight">53dp</item>
</style>
</resources>
在MyRatingBarDemo.java程序中
package com.tarena.ratingbar;
import android.app.Activity;
import android.database.CursorJoiner.Result;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
public class MyRatingBarDemo extends Activity {
private RatingBar ratingBar = null;
private TextView text = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.ratingBar = (RatingBar) super.findViewById(R.id.ratingbar);
this.text = (TextView) super.findViewById(R.id.text);
this.ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListenerImpl());
this.text.setMovementMethod(ScrollingMovementMethod.getInstance()); //设置文本可滚动
}
private class OnRatingBarChangeListenerImpl implements OnRatingBarChangeListener{
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
int num = (int) rating;
String result = null; //保存文字信息
switch (num) {
case 5:
result = "非常满意";
break;
case 4:
result = "满意";
break;
case 3:
result = "还可以";
break;
case 2:
result = "不满意";
break;
case 1:
result = "非常不满意";
break;
}
MyRatingBarDemo.this.text.setText(result);
}
}
}