Android控件之RatingBar自定义星级评分样式

 更新时间:2016年02月24日 11:55:54   作者:ForrestWoo  
RatingBar为评分条控件,默认效果为若干个绿色的星星,如果想将其换成其他自定义图片就要自定义它的style。接下来通过本文给大家介绍Android控件之RatingBar自定义星级评分样式,感兴趣的朋友一起学习吧

一、RatingBar简单介绍

RatingBar是基于SeekBar(拖动条)和ProgressBar(状态条)的扩展,用星形来显示等级评定,在使用默认RatingBar时,用户可以通过触摸/拖动/按键(比如遥控器)来设置评分, RatingBar自带有两种模式 ,一个小风格 ratingBarStyleSmall,大风格为ratingBarStyleIndicator,大的只适合做指示,不适用与用户交互。

效果图展示:

二、实例

1.布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:paddingLeft="10dip"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RatingBar android:id="@+id/ratingbar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="3"
android:rating="2.5" />
<RatingBar android:id="@+id/ratingbar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="2.25" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip">
<TextView android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RatingBar android:id="@+id/small_ratingbar"
style="?android:attr/ratingBarStyleSmall"
android:layout_marginLeft="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
<RatingBar android:id="@+id/indicator_ratingbar"
style="?android:attr/ratingBarStyleIndicator"
android:layout_marginLeft="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>

2.Java代码

package wjq.WidgetDemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.RatingBar.OnRatingBarChangeListener;
public class RatingBarDemo extends Activity implements
OnRatingBarChangeListener {
private RatingBar mSmallRatingBar;
private RatingBar mIndicatorRatingBar;
private TextView mRatingText;
/*
* (non-Javadoc)
* 
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ratingbarpage);
mRatingText = (TextView) findViewById(R.id.rating);
// We copy the most recently changed rating on to these indicator-only
// rating bars
mIndicatorRatingBar = (RatingBar) findViewById(R.id.indicator_ratingbar);
mSmallRatingBar = (RatingBar) findViewById(R.id.small_ratingbar);
// The different rating bars in the layout. Assign the listener to us.
((RatingBar)findViewById(R.id.ratingbar1)).setOnRatingBarChangeListener(this);
((RatingBar)findViewById(R.id.ratingbar2)).setOnRatingBarChangeListener(this);
}
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
final int numStars = ratingBar.getNumStars();
mRatingText.setText( 
" 受欢迎度" + rating + "/" + numStars);
// Since this rating bar is updated to reflect any of the other rating
// bars, we should update it to the current values.
if (mIndicatorRatingBar.getNumStars() != numStars) {
mIndicatorRatingBar.setNumStars(numStars);
mSmallRatingBar.setNumStars(numStars);
}
if (mIndicatorRatingBar.getRating() != rating) {
mIndicatorRatingBar.setRating(rating);
mSmallRatingBar.setRating(rating);
}
final float ratingBarStepSize = ratingBar.getStepSize();
if (mIndicatorRatingBar.getStepSize() != ratingBarStepSize) {
mIndicatorRatingBar.setStepSize(ratingBarStepSize);
mSmallRatingBar.setStepSize(ratingBarStepSize);
}
}
}

关于Android控件之RatingBar自定义星级评分样式就给大家介绍这里,希望对大家有所帮助,本文写的不好还请各位大侠多多指教!

相关文章

  • Android实现滤镜效果ColorMatrix

    Android实现滤镜效果ColorMatrix

    这篇文章主要为大家详细介绍了Android实现滤镜效果ColorMatrix,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-05-05
  • Android自定义ScrollView使用自定义监听

    Android自定义ScrollView使用自定义监听

    这篇文章主要介绍了Android自定义ScrollView使用自定义监听 ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • Android自定义ViewFlipper实现滚动效果

    Android自定义ViewFlipper实现滚动效果

    这篇文章主要为大家详细介绍了Android自定义ViewFlipper实现滚动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-08-08
  • Android 自定义通用的loadingview实现代码

    Android 自定义通用的loadingview实现代码

    本篇文章主要介绍了Android 自定义通用的loadingview实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-01-01
  • Android中 service组件详解

    Android中 service组件详解

    Service是Android的四大组件之一,以下是我结合Android Doc和网上资料的学习总结,有不准确的地方请高手指出,互相学习嘛。。。
    2016-08-08
  • Android ApplicationContext接口深入分析

    Android ApplicationContext接口深入分析

    ApplicationContext是Spring应用程序中的中央接口,由于继承了多个组件,使得ApplicationContext拥有了许多Spring的核心功能,如获取bean组件,注册监听事件,加载资源文件等
    2022-11-11
  • flutter 输入框组件TextField的实现代码

    flutter 输入框组件TextField的实现代码

    这篇文章主要介绍了flutter 输入框组件TextField的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • Kotlin中实体类的创建方式

    Kotlin中实体类的创建方式

    这篇文章主要介绍了Kotlin中实体类的创建方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • Android应用程序的编译流程及使用Ant编译项目的攻略

    Android应用程序的编译流程及使用Ant编译项目的攻略

    这篇文章主要介绍了Android应用程序的编译流程及使用Ant编译项目的攻略,Ant是集编译测试部署于一体的Java自动化工具,要的朋友可以参考下
    2016-04-04
  • Android实现简单卡片布局

    Android实现简单卡片布局

    这篇文章主要为大家详细介绍了Android实现卡片布局的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-10-10

最新评论