星級評分進度條(RatingBar)


星級評分進度條(RatingBar):(主要用於評價等方面)

常用的xml屬性;

android:isIndicator:RatingBar是否是一個指示器(用戶無法進行更改)

android:numStars:顯示的星星數量,必須是一個整型值,如“100”。

android:rating:默認的評分,必須是浮點類型,像“1.2”。

android:stepSize:評分的步長,必須是浮點類型,像“1.2”。

常用的方法:

監聽方法:setOnRatingBarChangelistener 

監聽器:RatingBar.OnRatingBarChangeListener

下面我們直接上代碼:

1.Activity

//星級評分條
public class RatingBarActivity extends Activity {

    private Context context;
    private RatingBar ratingBar;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rating_bar);
        
        context = this;
        ratingBar = (RatingBar)findViewById(R.id.room_ratingbar);
        
        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            public void onRatingChanged(RatingBar ratingBar, float rating,
                    boolean fromUser) {
                Toast.makeText(context, rating+"", Toast.LENGTH_SHORT).show();
            }
        });

    }

}

2.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp" >
<!-- 星級評分條 -->
    <RatingBar
        android:id="@+id/ratingBarId1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:isIndicator="true"
        android:numStars="5"
        android:rating="1.5" />

    <RatingBar
        android:id="@+id/room_ratingbar"
        style="@style/roomRatingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/ratingBarId1"
        android:layout_marginLeft="10dp"
        android:numStars="5"
        android:stepSize="1">
    
    </RatingBar>

</RelativeLayout>

3.style="@style/roomRatingBar"的布局文件

<style name="roomRatingBar" parent="@android:style/Widget.RatingBar">
        <item name="android:progressDrawable">@drawable/aaa</item>
        <item name="android:minHeight">20dp</item>
        <item name="android:maxHeight">20dp</item>
    </style>
</resources>

4.星星圖片的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/u53">
    </item>

    <item
        android:id="@+android:id/progress"
        android:drawable="@drawable/u45">
    </item>

</layer-list>

5.效果顯示圖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM