Android RatingBar 显示超过 5 颗星

标签 android

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

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();

}
}

我已经尝试对评级栏使用静态布局,但是当我这样做并尝试通过警告对话框显示评级栏时,没有显示星号。这是评分栏的布局文件:

<?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>

最佳答案

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

关于Android RatingBar 显示超过 5 颗星,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15504664/

相关文章:

android - 如何使用 AWS Pinpoint 通过 postman 发送推送通知

android - 异步任务内存泄漏

android - 仅通过 adb 杀死 android monkey 进程

java - 如何向 Eclipse 项目添加外部依赖项?

android - 边缘切入的背景/自定义背景形状

java - 如何使用 SQLiteOpenHelper 将参数传递给 onOpen() 函数

android - 即使应用程序在后台,如何启动相同的 Activity ?

android - 如何使用 Anko doAsync 处理 UI 线程上的 Kotlin-Room 异常

android - 如何在三个不同的 fragment 上观察来自实时数据android的数据

android - 如何在 Linux 平台上管理 mmap 的内存使用?