Android基础控件RatingBar星级评分条的使用
1、简介
RatingBar继承ProgressBar,除了ProgressBar的属性外还有特有属性:
android:isIndicator:是否用作指示,用户无法更改,默认false android:numStars:显示多少个星星,必须为整数 android:rating:默认评分值,必须为浮点数 android:stepSize: 评分每次增加的值,必须为浮点数 OnRatingBarChangeListener //事件处理
2、简单使用
1)星条的drawable资源layer-list图层:
<?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/rating_off"/> <item android:id="@android:id/progress" android:drawable="@drawable/rating_on"/> </layer-list>
2)在values文件夹下styles.xml文件中添加style:
<style name="roomRatingBar_1" parent="@android:style/Widget.RatingBar"> <item name="android:progressDrawable">@drawable/ratingbar_layer</item> <item name="android:minHeight">24dip</item> <item name="android:maxHeight">24dip</item> </style>
3)星条的xml布局文件:
<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:gravity="center" android:orientation="vertical" 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=".LoginActivity"> <RatingBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ratingbar" style="@style/roomRatingBar_1" android:isIndicator="false" android:numStars="6" android:rating="1.5" android:stepSize="0.25"/> </LinearLayout>
4)Java文件事件处理:
public class LoginActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); // Set up the login form. RatingBar ratingBar = (RatingBar)findViewById(R.id.ratingbar); ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float v, boolean b) { Toast.makeText(LoginActivity.this,"rating"+String.valueOf(v),Toast.LENGTH_SHORT).show(); } }); } }
ForeverGuard博客园