我正在开发一款使用自定义RatingBar的Android应用。我遵循了一个教程(http://kozyr.zydako.net/2010/05/23/pretty-ratingbar/),但是当我使用我的自定义样式时,它总是向我显示5颗星,甚至我强制将RatingBar强制为一个值(Ex 3星)。我在网上验证了其他一些教程,但它没有改变任何东西..
我的档案:
custom_ratingbar_full.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/background"
android:drawable="@drawable/custom_ratingbar_full_empty" />
<item android:id="@+id/secondaryProgress"
android:drawable="@drawable/custom_ratingbar_full_empty" />
<item android:id="@+id/progress"
android:drawable="@drawable/custom_ratingbar_full_filled" />
</layer-list>
custom_ratingbar_full_empty.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="@drawable/star_off" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="@drawable/star_off" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="@drawable/star_off" />
<item android:drawable="@drawable/star_off" />
</selector>
custom_ratingbar_full_filled.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="@drawable/star_full" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="@drawable/star_full" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="@drawable/star_full" />
<item android:drawable="@drawable/star_full" />
</selector>
styles.xml
<style name="CustomRatingBar" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/custom_ratingbar_full</item>
<item name="android:minHeight">48dip</item>
<item name="android:maxHeight">48dip</item>
</style>
my_fragment.xml
...
<RatingBar
android:id="@+id/rating_bar"
style="@style/CustomRatingBar"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:isIndicator="true"
android:layout_marginBottom="10dp"
android:visibility="invisible" />
...
有任何解释吗?我搜索了我做错了什么,但我找不到它..
编辑:
我如何设定评级值:
我在片段之间发送一个包
myRating = bundle.getInt("rating");
我检索RatingBar:
RatingBar ratingBar = (RatingBar) rootView.findViewById(R.id.rating_beer);
最后,我测试值是否显示评级栏:
if (beerRating != 0) {
ratingBar.setRating(myRating);
ratingBar.setVisibility(View.VISIBLE);
Toast.makeText(getActivity(), "Rating : " + myRating, Toast.LENGTH_LONG).show();
}
我已经验证了我的价值,并没有向我显示正确数量的星星。但是,如果我从评级栏中删除我的自定义样式,它就会起作用。
编辑2解决方案:
最后我找到了解决方案:我已将标签@+android:id
替换为@+id
,这就是为什么它不起作用的原因。
答案 0 :(得分:2)
我遇到了同样的问题。我下载了代码,自定义评级栏文件中的项目就像这样
<item
android:id="@+android:id/background"
android:drawable="@drawable/star_blank"/>
删除错误我将id更改为
<item
android:id="@+id/background"
android:drawable="@drawable/star_blank"/>
删除了错误,但它只显示所选项目的星号,但评级栏值正在变化。
所以我将自定义评级栏中的ID更改为
<item
android:id="@android:id/background"
android:drawable="@drawable/star_blank"/>
完美无缺。
答案 1 :(得分:0)
尝试此操作,将可绘制的进度设置为评级栏的属性,
<RatingBar
android:numStars="5"
android:progressDrawable="@drawable/ratingbar_selector"
android:stepSize="0.2" />