简体   繁体   English

Android RatingBar显示超过5星

[英]Android RatingBar showing more than 5 stars

I want to show a rating bar via an alert dialog in my android app. 我想在我的Android应用程序中通过警告对话框显示评级栏。 The problem I am facing is that depending upon the width of the screen, the ratingbar shows more than 5 stars(upto 10) in landscape mode and the function setNumStars() has no effect. 我面临的问题是,根据屏幕的宽度,评级栏在横向模式下显示超过5星(最多10个),而函数setNumStars()无效。 There are posts already dealing with this issue but they deal with a ratingbar whose laout is defined statically while I am creating it dynamically. 有些帖子已经处理过这个问题,但它们处理的是一个评级栏,其中laout是静态定义的,而我是动态创建它的。 How to solve this problem? 如何解决这个问题呢?

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

}
}

I have tried using static layout for the ratingbar but when I do this and try to display the ratingbar via an alert dialog, no stars are shown. 我已尝试使用静态布局进行评级栏,但是当我这样做并尝试通过警告对话框显示评级栏时,不会显示任何星标。 Here is the layout file for rating bar: 这是评级栏的布局文件:

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

setting the width of the RatingBar to "wrap content" solves the whole issue. 将RatingBar的宽度设置为“换行内容”可以解决整个问题。 According to the documentation: 根据文件:

Sets the number of stars to show. 设置要显示的星数。 In order for these to be shown properly, it is recommended the layout width of this widget be wrap content. 为了正确显示这些内容,建议将此窗口小部件的布局宽度设置为包装内容。

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

Try to set max value for your RatingBar Like Below 尝试为您的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>

create an xml file in your layout file and set this as the content 在布局文件中创建一个xml文件,并将其设置为内容

Change your showDialog function like this 像这样更改showDialog函数

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

keep parent of rating bar as linear layout and keep nothing else in that linear layout, that worked for me 保持评级栏的父级为线性布局,并保留该线性布局中的其他内容,这对我有用

This blog shows the clear picture of solution 这篇博客展示了清晰的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM