【问题标题】:Android RatingBar showing more than 5 starsAndroid RatingBar 显示超过 5 颗星
【发布时间】:2013-03-19 16:10:18
【问题描述】:

我想通过我的 android 应用程序中的警报对话框显示评分栏。我面临的问题是,根据屏幕的宽度,评分栏在横向模式下显示超过 5 颗星(最多 10 颗),并且函数 setNumStars() 没有效果。已经有一些帖子处理了这个问题,但是他们处理了一个 ratingbar,它的 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>

【问题讨论】:

  • 尝试设置步长为0.1

标签: android


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    将 RatingBar 的宽度设置为“包装内容”可以解决整个问题。 根据文档:

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

    【讨论】:

      【解决方案3】:

      尝试为您的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();
      

      【讨论】:

      • 这些更改无效。
      • 当我在 xml 文件中声明并将内容设置为正常工作时,如果您愿意,我可以发布代码
      • 是的,如果你没问题的话
      • 我正在尝试通过警报对话框显示评分栏,如果我使用您提到的静态布局,警报对话框上不会显示评分栏。请参阅 showRatingDialog 功能
      • 有效!但正确的代码是:rater.setView(getLayoutInflater().inflate(R.layout.your_ratting_layout_id, null));请编辑您的答案,我会标记它,谢谢!
      【解决方案4】:

      将评分栏的父级保留为线性布局,并在该线性布局中不保留任何其他内容,这对我有用

      This 博客显示解决方案的清晰图片

      【讨论】:

      • 完美运行,我不知道为什么没有赞成票。
      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 2011-04-20
        • 1970-01-01
        • 2015-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-06
        相关资源
        最近更新 更多