我必须动态创建评级栏,其中要设置的星数从服务器传递。在这里,我得到要设置的星数的值,并将其分配给setNumStars()
方法。我的代码和生成的设计如下所示。
在这里,我得到了n个星星。谢谢。 `
if(FO.answerType[i].equals("S")){
TextView quest = new TextView(this);
quest.setText(FO.question[i]);
quest.setTextSize(textsize);
quest.setTypeface(Typeface.DEFAULT_BOLD);
quest.setTextColor(Color.BLACK);
quest.setPadding(10, 6, 10, 6);
quest.setGravity(Gravity.LEFT);
lila.addView(quest);
RatingBar rate = new RatingBar(this);
rate.setNumStars(Integer.parseInt(FO.nos[i]));
lila.addView(rate);
}`
答案 0 :(得分:3)
将Layout Params
保持为WRAP_CONTENT
。它会起作用,比如
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.container);
RatingBar rate = new RatingBar(this);
rate.setRating(2);
rate.setNumStars(4);
rate.setProgress(1);
rate.setLayoutParams(new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT));
frameLayout.addView(rate);
答案 1 :(得分:1)
尝试.setRating(浮点值),在这里你有一个相关的问题:Set value in RatingBar
您可以正确实施RatingBar和setRating方法。
答案 2 :(得分:1)
Just RatingBar应该放在布局中,布局中有一个属性wrap_content。 魔术不存在。检查返回:
的Integer.parseInt(FO.nos [I])
突然有0。 See it所以。
答案 3 :(得分:1)
试试这个:
rate.setRating(Float.valueOf(FO.nos[i]));
编辑动态搜索栏:
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {
Context context;
List<PostVO> movieList;
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
LinearLayout ll;
RatingBar rb;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
ll = (LinearLayout) itemView.findViewById(R.id.ll);
rb = new RatingBar(context);
rb.setNumStars(10);
rb.setStepSize(1);
ll.addView(rb);
}
@Override
public void onClick(View v) {
Snackbar.make(v, movieList.get(getAdapterPosition()).getTitle(), Snackbar.LENGTH_SHORT).show();
}
}
public MovieAdapter(Context context,List<PostVO> movieList)
{
this.context = context;
this.movieList = movieList;
}
@Override
public MovieAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_movie_list, viewGroup, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(MovieAdapter.ViewHolder viewHolder, int i) {
viewHolder.rb.setRating(Float.valueOf(movieList.get(i).getRating()));
}
@Override
public int getItemCount() {
if(movieList != null && !movieList.isEmpty())
{
return movieList.size();
}
else {
return 0;
}
}
}
答案 4 :(得分:0)
您需要为此添加以下代码 -
RatingBar ratingBar = new RatingBar(this);
ratingBar.setRating(5);