RatingBar的使用

一、RatingBar幾個常用屬性
  • numStars:星的總數量
  • stepSize:變化的步長
  • isIndicator:false:允許拖動改變評分條 true:不允許通過拖動改變評分條
  • rating:初始的星數量

要想RatingBar顯示設置的星總數,它的layout_width必須設置為wrap_content

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
      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"
      tools:context="cn.ucai.day05_12_02_style.RatingbarActivity">
      <RatingBar
          android:id="@+id/ratingBar"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:numStars="5"
          android:isIndicator="false"
          android:rating="3"
          android:stepSize="0.5"/>
</RelativeLayout>

星的總數為5,初次顯示3顆星,效果圖


RatingBar.png

RatingBar監聽事件RatingBar.OnRatingBarChangeListener

RatingBar bar= (RatingBar)findViewById(R.id.ratingBar);
bar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener()
 {
    @Override
    public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
        if(b){
           Toast.makeText(RatingbarActivity.this,"拖動星星數量"+v,Toast.LENGTH_SHORT).show();        
}
}});
二、自定義RatingBar幾個常用屬性

自定義RatingBar關鍵在于使用標簽<layer-list/>以將多個圖片按照順序層疊起來。
分別設置固定id屬性為@android:id/background和進度@android:id/progress

<?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_unselected"/>
    <item
        android:id="@android:id/progress"
        android:drawable="@drawable/star_selected"/>
</layer-list>
效果圖
自定義RatingBar.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容