android - 自定义样式的 RatingBar 不显示

标签 android android-layout ratingbar

我已经在 http://kozyr.zydako.net/2010/05/23/pretty-ratingbar/ 之后为 RatingBar 创建了自定义样式当我在我的 XML 文件中声明我的 RatingBar 时,这非常有效。但是,我还需要在我的 Java 代码中创建动态评级栏,这是我执行此操作的地方:

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

问题是当我以这种方式使用它们时,评级栏不显示。这是我的自定义样式:

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

当我为评分条使用标准的小样式时,相同的过程非常有效。有人知道为什么吗?

最佳答案

我不知道为什么会这样,但作为解决方法,您可以在布局中声明您的 RatingBar 并在添加之前对其进行膨胀:

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

关于android - 自定义样式的 RatingBar 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19010865/

相关文章:

android - 通过 TCP 套接字连接两个安卓设备

android - 布局已经存在使用不同的限定符

android - 如何在 TextView 中显示评分栏值

Android如何增加评分栏中星星的高度和宽度

java - AlertDialog 中的评级栏 : How to change the number of stars programmatically?

android - 尝试发送 gcm 消息时的 HttpRetryExcepiton

java - Android 应用程序在多次切换 fragment 时崩溃

android - 如何在android中的 ListView 中显示联系人详细信息以及图像

android - native Android UI 组件未更新

java - 以编程方式设置 PercentageRelativeLayout 中的宽高比