代码中: mRatingBar.setRating(3.8f);
代码:
package com.example.sj.xiaoxingxing2;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.TextView;
public class MainActivity extends Activity {
private Context context;
private RatingBar mRatingBar ;
private TextView mRatingBarLevel ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// context = this;
mRatingBar = (RatingBar)findViewById(R.id.service_grade_ratingBar);//星
mRatingBarLevel = (TextView)findViewById(R.id.service_grade_level);//星个数
mRatingBar.setRating(3.8f);
// mRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
// @Override
// public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
// float i = mRatingBar.getRating();
// mRatingBarLevel.setText(i + "");
// }
// });
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sj.xiaoxingxing2.MainActivity">
<!-- 星级评分条 -->
<RatingBar
android:id="@+id/service_grade_ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:numStars="5"
android:rating="1.5" />
<TextView
android:id="@+id/service_grade_level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>