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 viewpager 3D画廊的实现方法

    Android viewpager 3D画廊的实现方法

    ViewPager在开发中的使用频率非常的高,接下来通过本文给大家分享android viewpager 3D画廊的实现方法,需要的朋友参考下吧
    2017-02-02
  • Android性能优化方案详情

    Android性能优化方案详情

    这篇文章主要给大家分享的是Android项目工程内的一些性能优化方式,文章围绕Android项目工程优化方式展开内容,需要的朋友可以参考一下文章的具体详情,希望对你有所帮助
    2021-11-11
  • Android 源码浅析RecyclerView Adapter

    Android 源码浅析RecyclerView Adapter

    这篇文章主要介绍了Android 源码浅析之RecyclerView Adapter示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • Android数字华容道小游戏开发

    Android数字华容道小游戏开发

    这篇文章主要为大家详细介绍了Android数字华容道小游戏开发的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • Android加载loading对话框的功能及实例代码(不退出沉浸式效果)

    Android加载loading对话框的功能及实例代码(不退出沉浸式效果)

    这篇文章主要介绍了Android加载loading对话框的功能及实例代码,不退出沉浸式效果,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-12-12
  • Android实现顶部悬浮效果

    Android实现顶部悬浮效果

    这篇文章主要为大家详细介绍了Android实现顶部悬浮效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • 使用Compose制作抖音快手视频进度条Loading动画效果

    使用Compose制作抖音快手视频进度条Loading动画效果

    这篇文章主要为大家介绍了使用Compose制作抖音快手视频进度条Loading动画效果,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • 深入android中The connection to adb is down的问题以及解决方法

    深入android中The connection to adb is 

    本篇文章是对android中The connection to adb is down的问题以及解决方法进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • JankMan-极致的卡顿分析系统

    JankMan-极致的卡顿分析系统

    这篇文章主要为大家介绍了JankMan-极致的卡顿分析系统使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • Android入门之使用OKHttp组件访问网络资源

    Android入门之使用OKHttp组件访问网络资源

    这篇文章主要为大家详细介绍了Android如何使用OKHttp组件访问网络资源功能,文中的示例代码讲解详细,具有一定的借鉴价值,需要的可以参考一下
    2022-12-12

最新评论