繁体   English   中英

将初始值设置为android的评级栏中的第1颗星

[英]Set initial value to 1st star in rating bar of android

我的android评级栏有以下代码:

<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="28dp"
android:stepSize="1.0" />

我想将第一个星值初始化为-5,这样剩下的星星就会得到像-4,-3,-2这样的值......
但是我不知道如何将这个初始值给予android中我的评级栏的第一颗星。
我希望我的评级栏有三种颜色:

  • 对于值-5,-4,-3,-2,-1,星色应为红色,
  • 对于值0星的颜色应为蓝色
  • 对于值1,2,3,4,5,星形颜色应为绿色。

最低评级可以是0,不允许使用负数。

但是,您可以创建一个包含11颗星的评级栏来表示值(-5到+5)

在评级栏的监听器中,将值映射到-5到+5的范围(通过从接收的参数中减去6)动态更改颜色,如下所示:

  • 0:蓝色
  • 负值:红色
  • 正值:绿色

所以,输出看起来像这样:

在此输入图像描述

活动:

import android.app.Activity;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {
      private RatingBar ratingBar;
      private TextView tvRating;
      private LayerDrawable stars;

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.original_activity_main);

        ratingBar = (RatingBar) findViewById(R.id.ratingBar);
        tvRating = (TextView) findViewById(R.id.value);
        stars = (LayerDrawable) ratingBar.getProgressDrawable();

        ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
            public void onRatingChanged(RatingBar ratingBar, float ratingValue,
                boolean fromUser) {

                int value = (int) (ratingValue) - 6;
                tvRating.setText(String.valueOf(value));

                int color = Color.BLUE;

                if(value > 0)
                    color = Color.GREEN;
                else if(value < 0)
                    color = Color.RED;

                    stars.getDrawable(2).setColorFilter(color, PorterDuff.Mode.SRC_ATOP);    
            }
        });
      }

    }

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Result : " />

    <TextView
        android:id="@+id/value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/label"
        android:text="" />

    <RatingBar
        android:id="@+id/ratingBar"
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/label"
        android:isIndicator="false"
        android:numStars="11"
        android:rating="0.0"
        android:stepSize="1.0" />

</RelativeLayout>

据我所知,您可以设置RatingBar的最小值是0(没有负数)。

如果将stepSize设置为1.0,则必须使用if条件设置ratingbar。

例如:

if(yourNumber ==(-5)){
ratingBar.setRating(1.0f); //you need to set it using a Float value
} else if (yourNumber ==(-4)){
ratingBar.setRating(2.0f);
} //And so on....

关于更改星形颜色 - 您需要定义自己的评级栏样式 - 阅读文章。

祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM