[英]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.