[英]Custom Rating Bar show one star only
.
。
I create a custom rating bar. 我创建了一个自定义评级栏。 I set 5 custom star images, but it shows only one.
我设置了5个自定义星形图像,但它只显示了一个。 Instead of a variable number of rating drawables, I see only one, regardless of the number of stars set.
而不是可变数量的评级可绘制,我只看到一个,无论设置的星数是多少。 Any help will be appreciated.
任何帮助将不胜感激。
xml.code : xml.code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<RatingBar
android:id="@+id/ratingbar_default"
style="@style/RatingBar"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginBottom="20dp" />
<TextView
android:id="@+id/textView"
android:layout_width="276dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="show state up checkbox"
android:textColor="#CC00CC"
android:textSize="20dp" />
java code : java代码:
Activity RatingBarActivity
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rating);
final TextView text = (TextView) findViewById(R.id.textView);
final RatingBar ratingBar_default = (RatingBar) findViewById(R.id.ratingbar_default);
ratingBar_default.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener()
{
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser)
{
// TODO Auto-generated method stub
text.setText("Rating: " + String.valueOf(rating));
}
});
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@android:id/background"
android:drawable="@drawable/star_ratingbar_full_empty" />
<item
android:id="@android:id/secondaryProgress"
android:drawable="@drawable/star_ratingbar_full_empty" />
<item
android:id="@android:id/progress"
android:drawable="@drawable/star_ratingbar_full_filled" />
</layer-list>
res/drawable/star_ratingbar_full_empty.xml: RES /绘制/ star_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/star2" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="@drawable/star2" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="@drawable/star2" />
<item android:drawable="@drawable/star2" />
</selector>
res/drawabstar_ratingbar_full_filled.xml: RES / drawabstar_ratingbar_full_filled.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- This is the rating bar drawable that is used to
show a unfilled cookie. -->
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="@drawable/star3" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="@drawable/star1" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="@drawable/star1" />
<item android:drawable="@drawable/star1" />
</selector>
styles.xml file : styles.xml文件:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"></style>
<style name="RatingBar" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/star_rating_bar_full</item>
<item name="android:minHeight">48dip</item>
<item name="android:maxHeight">48dip</item>
</style>
</resources>
In my case trouble was in SVG drawables in layer-list. 在我的情况下,故障是在图层列表中的SVG drawables。 When I changed them to PNG it starts work correctly
当我将它们更改为PNG时,它开始正常工作
Try to specify an android:numStars
attribute for your RatingBar in layout file activity_rating.xml
. 尝试在布局文件
activity_rating.xml
为RatingBar指定android:numStars
属性。 I guess, the default value is 1 - that's why you see only one star 我想,默认值是1 - 这就是为什么你只看到一颗星
Just remove any-dpi
drawable of rating bar filled and empty image from drawable. 只需从drawable中删除
any-dpi
drawable of rating bar和empty image。 And it will work for you. 它会对你有用。 Because it to show custom rating bar you can give both
xml
and png files in your layer list
. 因为要显示自定义评级栏,您可以在
layer list
同时提供xml
和png文件。 But to show all the stars, you have to put only png
icons into drawable
folder. 但要显示所有星星,你必须只将
png
图标放入drawable
文件夹。
Has been a while but never hurts to answer. 已经有一段时间但从来没有伤害过回答。 Here is my 3 step solution regardless of SDK version.
这是我的3步解决方案,无论SDK版本如何。
1- Add this as your RatingBar in your xml layout: 1-在xml布局中将其添加为RatingBar:
<RatingBar
android:id="@+id/new_ratingbar"
android:progressDrawable="@drawable/custom_drawable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="0.5"/>
If needed, you can give the RatingBar minHeight and maxHeight or add other attributes as required. 如果需要,您可以给出RatingBar minHeight和maxHeight,或者根据需要添加其他属性。
2- Here is the drawable you would refer the above android:progressDrawable to: 2-这里是drawable你会将上面的android:progressDrawable引用到:
custom_drawable.xml custom_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@android:id/background"
android:drawable="@drawable/rating_bar_empty" /> <!--this is your .png file-->
<item
android:id="@android:id/secondaryProgress"
android:drawable="@drawable/rating_bar_fill" /> <!--this is your .png file-->
<item
android:id="@android:id/progress"
android:drawable="@drawable/rating_bar_fill" /> <!--this is your .png file-->
</layer-list>
3- The last thing is to provide the .png files in your various drawable folders. 3-最后一件事是在各种可绘制文件夹中提供.png文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.