質問

I have created my custom style for RatingBar following http://kozyr.zydako.net/2010/05/23/pretty-ratingbar/ This works perfectly when I declare my RatingBar in my XML file. However I also need to create dynamic ratingbars in my java code, this is where I do it:

for (int i=0; i<Math.min(10, n); i++){
                TableRow row = new TableRow(RegisterActivity.this);
                TextView t = new TextView(RegisterActivity.this);     
                t.setText((i+1) + ". " + myCList.get(i) + " / " + myWList.get(i) + " (" + mySList.get(i) + ")");
                RatingBar c = new RatingBar(RegisterActivity.this, null, R.style.foodRatingBar);
                Double d=(Double) myRList.get(i);
                c.setRating(d.floatValue());
                c.setClickable(false);
                row.addView(t);
                row.addView(c);
                table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            }

The problem is that the ratingbars are not displayed when I use them in this way. This is my custom style:

<style name="foodRatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/food_ratingbar_full</item>
    <item name="android:minHeight">50dp</item>
    <item name="android:maxHeight">50dp</item>
</style>

The same procedure works perfectly when I use the standard small style for ratingbars. Does anybody know why?

役に立ちましたか?

解決

I don't know why it happens, but as workaround you can declare your RatingBar in layout and inflate it before adding:

<RatingBar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/foodRatingBar" />

RatingBar c = (RatingBar) iflater.inflate(R.layout.rating_bar, row, false);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top