Android评分条显示超过5颗星星

19

我希望在我的Android应用程序中通过警报对话框显示评分栏。 我面临的问题是,根据屏幕宽度,横向模式下评分栏显示超过5个星级(最多10个星级),而函数setNumStars()没有效果。 已经有帖子处理这个问题,但它们处理静态定义布局的ratingbar,而我是动态创建它。 如何解决这个问题?

public class MainActivity extends Activity {
private Button launchButton;
//Rating dialog
 private AlertDialog.Builder rater;
 private RatingBar ratingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    launchButton =(Button)findViewById(R.id.ratingButton);
    launchButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //Here the alert dialog will be launched
            showRatingDialog();
        }
    });

      //Setting up rating dialog
        rater = new AlertDialog.Builder(this);

        rater.setTitle("This is the rating dialog");


}

@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;
}
public void showRatingDialog()
{
  ratingBar = (RatingBar)findViewById(R.id.ratingStars);
      rater.setNegativeButton("Abbrechen", null);
      rater.setView(ratingBar);
      rater.show();

}
}

我尝试使用固定布局来创建RatingBar,但是当我通过警示对话框来显示RatingBar时,没有星星显示。这是RatingBar的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RatingBar
        android:id="@+id/ratingStars"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:stepSize="1"
        />

</LinearLayout>

尝试将步长设置为0.1。 - Triode
5个回答

24

设置 android:numStars="5"android:layout_width="wrap_content"


19

将RatingBar的宽度设置为“wrap content”即可解决整个问题。

设置要显示的星数。为了正确显示这些星星,建议此小部件的布局宽度为wrap content。


11
尝试为您的RatingBar设置最大值,就像下面这样。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/active_bg" >

    <RatingBar
            android:id="@+id/rating"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="?android:attr/ratingBarStyleSmall"
            android:numStars="5"
            android:stepSize="0.1"
            android:isIndicator="true" />

</RelativeLayout>

在您的布局文件中创建一个XML文件,并将其设置为内容

将您的showDialog函数更改为以下内容

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.***your_xml_name***));
editor.setView(layout);
editor.show();

1
这些更改没有效果。 - user1107888
当我在xml文件中声明并设置内容时,它能够正常工作。如果您需要,我可以发布代码。 - Triode
我正在尝试通过警示对话框显示评分栏,如果我像您所提到的那样使用静态布局,则没有评分栏显示在警示对话框上。请参见showRatingDialog函数。 - user1107888
1
它奏效了!但正确的代码是:rater.setView(getLayoutInflater().inflate(R.layout.your_rarting_layout_id, null)); 请编辑您的答案,我会标记它。谢谢! - user1107888
查看编辑后的答案,对不起,我以为你会想到,干得好 :) - Triode
显示剩余3条评论

3

将评分栏的父级保持为线性布局,并仅在该线性布局中不包含其他内容,这对我有效。

此博客展示了解决方案的清晰图片.


1
完美运行,我不知道为什么没有点赞。 - Ajay

2
如果您的RatingBar在AlertDialog中,将其放入LinearLayout中,并使用setView在LinearLayout上。然后,您可以将其宽度设置为WRAP_CONTENT。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接