我已经使用了警告对话框来获取星级评分栏。但我无法更改正在显示的星数(现在它显示6 ..我希望它为5)。
评级栏没有相应的XML,因为它是动态制作的。 我已经尝试了setNumStars()方法但没有用....还尝试更改为wrapcontent。
这是我的代码..请帮助
enter code here
protected void showRatingAlert() {
// TODO Auto-generated method stub
AlertDialog.Builder ab = new AlertDialog.Builder(AndroidLoadImageFromURLActivity.this);
ab.setTitle("Enter Rating");ab.setMessage("STARS");
final RatingBar rat = new RatingBar(AndroidLoadImageFromURLActivity.this);
rat.setNumStars(5);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
rat.setLayoutParams(lp);
ab.setView(rat);
ab.setIcon(R.drawable.ic_launcher);
ab.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
float val = rat.getRating();
if(ur.equals("0"))
{
nr = val;
}
else
{
nr = (art+val)/2;
}
答案 0 :(得分:0)
来自RatingBar
文档:
设置要显示的星数。为了正确显示这些内容,建议此窗口小部件的布局宽度为换行内容。
因此,在您的情况下,您可以创建布局,例如layout/rating.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RatingBar
android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:numStars="5"/>
</LinearLayout>
并将此布局用作对话框上下文。这是修改过的对话框的代码:
protected void showRatingAlert() {
// TODO Auto-generated method stub
AlertDialog.Builder ab = new AlertDialog.Builder(AndroidLoadImageFromURLActivity.this);
ab.setTitle("Enter Rating");ab.setMessage("STARS");
//inflating layout and finding RatingBar widget
View root = ((LayoutInflater) AndroidLoadImageFromURLActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.rating, null);
ab.setView(root);
final RatingBar rat = (RatingBar)root.findViewById(R.id.ratingbar);
rat.setNumStars(5);
ab.setIcon(R.drawable.ic_launcher);
//and all your existing code
...