我想通过我的 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>