将初始值设置为android评级栏中的第一颗星
我的安卓评级栏上有以下代码:将初始值设置为android评级栏中的第一颗星,android,rating,Android,Rating,我的安卓评级栏上有以下代码: <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:lay
<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…但我不知道如何将这个初始值赋予我在安卓系统中评级栏的第一颗星。
我希望我的评级栏有三种颜色:
- 对于值-5、-4、-3、-2、-1,星形颜色应为红色
- 对于值0,星形颜色应为蓝色
- 对于值1、2、3、4、5,星形应为绿色李>
步长设置为1.0,则必须使用If条件设置分级栏
例如:
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....
关于更改星形颜色-您需要定义自己的分级栏样式-请阅读本文
祝你好运 最低评级可以是0,不允许出现负数
但是,您可以创建一个带有11颗星的评级栏来表示值(-5到+5)
在评级栏的侦听器中,将值映射到-5到+5的范围(通过从接收到的参数中减去6),动态更改颜色,如下所示:
- 0:蓝色
- 负值:红色
- 正值:绿色
因此,输出将如下所示:
活动:
导入android.app.Activity;
导入android.graphics.Color;
导入android.graphics.PorterDuff;
导入android.graphics.drawable.LayerDrawable;
导入android.os.Bundle;
导入android.widget.RatingBar;
导入android.widget.RatingBar.OnRatingBarChangeListener;
导入android.widget.TextView;
公共类MainActivity扩展了活动{
私人评级杆评级杆;
私人文本视图电视收视率;
私人分层可绘制的星星;
@凌驾
创建时的公共void(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();
setOnRatingBarChangeListener(新的OnRatingBarChangeListener(){
公共无效比率已更改(比率条比率条、浮动比率值、,
布尔值(用户){
int值=(int)(评级值)-6;
tvRating.setText(String.valueOf(value));
int color=color.BLUE;
如果(值>0)
颜色=颜色。绿色;
else if(值<0)
颜色=颜色。红色;
stars.getDrawable(2).setColorFilter(color,PorterDuff.Mode.SRC_);
}
});
}
}
XML:
我在这里看不到任何问题。
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 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>