[英]Custom Rating Bar show one star only
。
我创建了一个自定义评级栏。 我设置了5个自定义星形图像,但它只显示了一个。 而不是可变数量的评级可绘制,我只看到一个,无论设置的星数是多少。 任何帮助将不胜感激。
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代码:
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 /绘制/ 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:
<?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文件:
<!-- 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>
在我的情况下,故障是在图层列表中的SVG drawables。 当我将它们更改为PNG时,它开始正常工作
尝试在布局文件activity_rating.xml
为RatingBar指定android:numStars
属性。 我想,默认值是1 - 这就是为什么你只看到一颗星
只需从drawable中删除any-dpi
drawable of rating bar和empty image。 它会对你有用。 因为要显示自定义评级栏,您可以在layer list
同时提供xml
和png文件。 但要显示所有星星,你必须只将png
图标放入drawable
文件夹。
已经有一段时间但从来没有伤害过回答。 这是我的3步解决方案,无论SDK版本如何。
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"/>
如果需要,您可以给出RatingBar minHeight和maxHeight,或者根据需要添加其他属性。
2-这里是drawable你会将上面的android:progressDrawable引用到:
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-最后一件事是在各种可绘制文件夹中提供.png文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.