繁体   English   中英

自定义RatingBar总是显示五颗星

[英]Custom RatingBar shows always five stars

我正在开发一款使用自定义RatingBar的Android应用。 我按照教程( http://kozyr.zydako.net/2010/05/23/pretty-ratingbar/ ),但当我使用我的自定义样式时,它总是显示我5星,即使我强迫RatingBar一个值(前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替换了标签@+android:id @+id ,这就是为什么它不起作用的原因。

我遇到了同样的问题。 我下载了代码,自定义评级栏文件中的项目是这样的

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

这很完美。

试试这个,将可绘制的进度设置为评级栏的属性,

                        <RatingBar

                        android:numStars="5"
                        android:progressDrawable="@drawable/ratingbar_selector"
                        android:stepSize="0.2" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM