Android开发总结笔记 RatingBar(评分条) 1-1-12

来源:互联网 发布:服装定制系统php源码 编辑:程序博客网 时间:2024/06/05 17:02

RatingBar(API)用星星的数量来进行评分显示。是个比较简单的组件。下面来看一看他的一些属性
  • android:isIndicator        设置Rating是否为指示器,意思是用户不可改变RatingBar的值
  • android:numStarts        设置RatingBar显示的星星的数量,整数型
  • android:rating                设置默认的评分,浮点型
  • android:stepSize            设置每一步的评分是多大,浮点型

1、RatingBar的几个样式
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical">
 
<RatingBar
style="@android:style/Widget.Holo.RatingBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5" />
<RatingBar
style="@android:style/Widget.Holo.RatingBar.Indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5" />
<RatingBar
style="@android:style/Widget.Holo.RatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5" />
 
 
</LinearLayout>



注意!RatingBar的width不能设置成match_parent,否则android:numStarts属性会失效。


2、给RatingBar添加触发事件
mRbTest = (RatingBar) findViewById(R.id.ratingBar);
mRbTest.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Toast.makeText(MainActivity.this, rating + "", Toast.LENGTH_LONG).show();
}
});

device-2015-10-12-092020.gif


3、自定义RatingBar样式

ratingbar.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/ic_rating_bar_off"></item>
<item android:id="@android:id/secondaryProgress" android:drawable="@drawable/ic_rating_bar_off"></item>
<item android:id="@android:id/progress" android:drawable="@drawable/ic_rating_bar_on"></item>
</layer-list>

customRatingBar.xml
<style name="customRatingBar" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/ratingbar</item>
<item name="android:minHeight">24dp</item>
<item name="android:minWidth">24dp</item>
</style>

<RatingBar
android:id="@+id/ratingBar"
style="@style/customRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5" />





0 0
原创粉丝点击