最近项目中用到RatingBar,系统提供的RatingBar 样式太难看了,于是乎自定义了一下RatingBar的样式,个人觉得自定义样式后的效果比系统的好看多了。废话不多说了,直接看效果吧
主界面布局
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" >
<RatingBar
android:id="@+id/rb_score"
style="@style/custom_ratingbar_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp" />
<TextView
android:id="@+id/tv_score"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text=""
android:textColor="@color/red"
android:textSize="13sp" />
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.customratingbar;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.RatingBar;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findView();
}
private void findView() {
RatingBar rb_score = (RatingBar) findViewById(R.id.rb_score);
TextView tv_score = (TextView) findViewById(R.id.tv_score);
float score = 3.2f;
rb_score.setRating(score);
tv_score.setText("评分:"+score);
}
@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;
}
}
自定义RatingBar样式
<style name="custom_ratingbar_style" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/custom_ratingbar_progress</item>
<item name="android:minHeight">17dip</item>
<item name="android:maxHeight">17dip</item>
<item name="android:stepSize">0.1</item>
<item name="android:isIndicator">true</item>
</style>
custom_ratingbar_progress.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_grey">
</item>
<item
android:id="@+android:id/secondaryProgress"
android:drawable="@drawable/star_grey">
</item>
<item
android:id="@+android:id/progress"
android:drawable="@drawable/star_red">
</item>
</layer-list>