简体   繁体   English

在RatingBar上调用getProgressDrawable()只显示一颗星

[英]Calling getProgressDrawable() on a RatingBar only displays one star

I have a custom RatingBar in my app that should display either the site average rating or the user rating for a movie if a user has rated it. 我的应用程序中有一个自定义RatingBar,如果用户对其进行评级,则应显示网站平均评分或电影的用户评级。 The color should change depending on whether the rating displayed is the site average or a user rating. 颜色应根据显示的评级是网站平均值还是用户评级而改变。

Here's the process: User views movie. 这是过程:用户观看电影。 Site average rating for the movie is displayed. 显示电影的网站平均评分。

Layout code for the RatingBar: RatingBar的布局代码:

<RatingBar android:id="@+id/ratingBar" android:isIndicator="true" 
    android:layout_margin="10dip" style="@style/RatingBar"
    android:progressDrawable="@drawable/site_rating_bar" android:stepSize="0.1" />

RatingBar style: RatingBar风格:

<style name="RatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:numStars">10</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:minHeight">30dip</item>
    <item name="android:maxHeight">30dip</item>
</style>

site_rating_bar.xml: site_rating_bar.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:id="@+android:id/background"
      android:drawable="@drawable/btn_rating_star_off" />
   <item android:id="@+android:id/secondaryProgress"
      android:drawable="@drawable/btn_rating_star_off" />
   <item android:id="@+android:id/progress"
      android:drawable="@drawable/btn_site_rating_star_on" />
</layer-list>

The user can click on the RatingBar and bring up a dialog where they can add their own custom rating of the movie. 用户可以点击RatingBar并打开一个对话框,在那里他们可以添加自己的电影自定义评级。 Upon close of the dialog, the code updates the rating displayed in the RatingBar above. 关闭对话框后,代码将更新上面RatingBar中显示的评级。 I'd like to also change the ProgressDrawable so that the color of the stars changes to indicate the user has added a custom rating. 我还想更改ProgressDrawable,以便星星的颜色发生变化,表明用户已添加自定义评级。

But when I try to set the ProgressDrawable programmatically it does update it so it's displaying the right color, but it only displays a single star, stretched across the screen. 但是当我尝试以编程方式设置ProgressDrawable时它会更新它以便显示正确的颜色,但它只显示一个横跨屏幕的星形。

ratingBar.setProgressDrawable(thisActivity.getResources().getDrawable(R.drawable.user_rating_bar));

user_rating_bar.xml: user_rating_bar.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+android:id/background"
      android:drawable="@drawable/btn_rating_star_off" />
    <item android:id="@+android:id/secondaryProgress"
      android:drawable="@drawable/btn_rating_star_off" />
    <item android:id="@+android:id/progress"
      android:drawable="@drawable/btn_user_rating_star_on" />
</layer-list>

I've tried saving the original bounds and resetting them, but it didn't help: 我已经尝试保存原始边界并重置它们,但它没有帮助:

Rect bounds = ratingBar.getProgressDrawable().getBounds();  
ratingBar.setProgressDrawable(thisActivity.getResources().getDrawable(R.drawable.user_rating_bar));
ratingBar.getProgressDrawable().setBounds(bounds);

Also tried resetting the numStars in case it had gotten lost. 还尝试重置numStars以防它丢失。

Any ideas greatly appreciated. 任何想法都非常感激。 TIA. TIA。 :) :)

I'm going through this same thing right now...I can't for the life of me get it to work as expected through code, so I'm going the janky-layout route. 我现在正在经历同样的事情......我不能为我的生活让它按照预期的方式通过代码工作,所以我要去janky-layout路线。 :( :(

<RatingBar
  android:id="@+id/beer_rating"
  android:numStars="5"
  android:progressDrawable="@drawable/rating_bar"
  android:indeterminateDrawable="@drawable/rating_bar"
  android:stepSize="0.5"
  android:isIndicator="true"
  android:layout_gravity="center"
  style="?android:attr/ratingBarStyleSmall" />
<RatingBar
  android:id="@+id/beer_rating_average"
  android:indeterminateDrawable="@drawable/rating_bar_average"
  android:isIndicator="true"
  android:layout_gravity="center"
  android:numStars="5"
  android:progressDrawable="@drawable/rating_bar_average"
  android:stepSize="0.5"
  android:visibility="gone"
  style="?android:attr/ratingBarStyleSmall"/>

Then just flipping the visibility on them when needed: 然后只需在需要时翻转它们的可见性:

findViewById(R.id.beer_rating).setVisibility(View.GONE);
findViewById(R.di.beer_rating_average).setVisibility(View.VISIBLE);

Sucks, but it works. 糟透了,但它确实有效。 Let me know if you come up with any other solves. 如果你想出任何其他的解决方案,请告诉我。

Try using: Use ProgressBar.setProgressDrawableTiled(Drawable) 尝试使用:使用ProgressBar.setProgressDrawableTiled(Drawable)

and set minSDK to 21 并将minSDK设置为21

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

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