新思想

RatingBar

RatingBar

 

styles.xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="roomRatingBar" parent="@android:style/Widget.RatingBar">
        <item name="android:progressDrawable">@drawable/ratingbar_full</item>
        <item name="android:minHeight">24dip</item>
        <item name="android:maxHeight">24dip</item>
    </style>
</resources>

ratingbar_full.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="@mipmap/ic_rating_off" />
    <item android:id="@android:id/secondaryProgress"
        android:drawable="@mipmap/ic_rating_off" />
    <item android:id="@android:id/progress"
        android:drawable="@mipmap/ic_rating_on" />
</layer-list>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.xiesir.example12ratingbar.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="一、基本属性:\n
style: 设置样式\n
progressTint: 设置颜色\n
isIndicator:是否用作指示,而用户无法更改\n
numStars:显示多少个星星\n
rating:默认评分值\n
stepSize:每次增加的值\n\n
OnRatingBarChangeListener事件中重写onRatingChanged()方法" />

    <RatingBar
        style="@style/Widget.AppCompat.RatingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RatingBar
        android:id="@+id/rbnormal"
        style="@style/Base.Widget.AppCompat.RatingBar.Indicator"
        android:progressTint="#007FFF"
        android:isIndicator="false"
        android:numStars="10"
        android:rating="8.5"
        android:stepSize="0.5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RatingBar
        style="@style/Widget.AppCompat.RatingBar.Small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RatingBar
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RatingBar
        style="?android:attr/ratingBarStyleIndicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RatingBar
        style="?android:attr/ratingBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:text="自定义属性"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RatingBar
        style="@style/roomRatingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

MainActivity.java:

package com.xiesir.example12ratingbar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RatingBar rbnormal = (RatingBar) findViewById(R.id.rbnormal);
        rbnormal.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                Toast.makeText(MainActivity.this, "rating: " + String.valueOf(rating), Toast.LENGTH_LONG).show();
            }
        });

    }
}

源程序下载

参考:

posted on 2016-05-17 01:40  新思想  阅读(460)  评论(0)    收藏  举报

导航