RatingBar舍入给定的浮点数

时间:2015-11-27 08:11:52

标签: android ratingbar

我正在使用构建工具23.0.2,而我试图给我的RatingBar视图一个浮点数(即2.5),我期待一个半星,因为我的步长指定。

<RatingBar
    android:id="@+id/rating_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:rating="0"
    android:scaleX="0.7"
    android:scaleY="0.7"
    android:stepSize="0.5"/>

但它总是显示圆形的星星!我检查了源代码,发现setProgress()方法使用的setRating()方法只接受一个整数!

2 个答案:

答案 0 :(得分:4)

enter image description here

第1步:打开res - &gt;布局 - &gt; activity_main.xml并添加以下代码:

<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="com.skholingua.android.custom_ratingbar.MainActivity" >

    <RatingBar
        android:id="@+id/ratingBar5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true" />

    <RatingBar
        android:id="@+id/ratingBar4"
        style="@style/StarRatingBar"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_below="@+id/textView5"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="98dp"
        android:text="Custom RatingBar with Color"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ratingBar5"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="104dp"
        android:text="Custom RatingBar with Images"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

第2步:打开res - &gt; drawable - &gt;创建新的xml,ratingbar_selector_images.xml.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/skholinguaicon_blue"/>
        <item
            android:id="@+android:id/secondaryProgress"
            android:drawable="@drawable/skholinguaicon_blue"/>
        <item
            android:id="@+android:id/progress"
            android:drawable="@drawable/skholinguaicon"/>

    </layer-list>

第3步:打开res - &gt;值 - &gt; styles.xml并添加以下代码:     

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.

-->
<style name="AppBaseTheme" parent="android:Theme.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.

    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

<style name="StarRatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/ratingbar_selector_images</item>
    <item name="android:minHeight">48dip</item>
    <item name="android:maxHeight">48dip</item>
</style>

</resources>

第4步:打开src - &gt;包 - &gt; MainActivity.java并添加以下代码:     包com.skholingua.android.custom_ratingbar;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RatingBar;

public class MainActivity extends Activity {
    private RatingBar rb_customColor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rb_customColor = (RatingBar) findViewById(R.id.ratingBar5);

        /*
         * For custom color only using layerdrawable to fill the star colors
         */
        LayerDrawable stars = (LayerDrawable) rb_customColor
                .getProgressDrawable();
        stars.getDrawable(2).setColorFilter(Color.parseColor("#26ce61"),
                PorterDuff.Mode.SRC_ATOP); // for filled stars
        stars.getDrawable(1).setColorFilter(Color.parseColor("#FFFF00"),
                PorterDuff.Mode.SRC_ATOP); // for half filled stars
        stars.getDrawable(0).setColorFilter(Color.CYAN,
                PorterDuff.Mode.SRC_ATOP); // for empty stars

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

步骤5:打开AndroidManifest.xml并添加以下代码:          

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

答案 1 :(得分:0)

嗨,这段代码对我来说很好:

  <RatingBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/myratingBar"
    android:numStars="5"
    style="?android:attr/ratingBarStyleSmall"
    android:textAlignment="center"
    android:layout_gravity="center"
android:progressBackgroundTint="#000000" />


RatingBar rating = (RatingBar)vi.findViewById(R.id.myratingBar);
LayerDrawable stars = (LayerDrawable) rating.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
rating.setRating((float) 4.5);

使用LayerDrawable自定义您的RatingBar