2014-03-30 54 views
1

是否可以創建一個像這樣的自定義評級欄? 如果是的話,你能舉個例子說明如何去做嗎?與自定義圖像的評級欄

enter image description here

提前感謝!

+0

你想要你自定義評級欄在你的佈局? – joao2fast4u

+0

在對話框中,但這並不重要imo – TGIO

回答

-3

編輯:這不是一個合適的答案,因爲OP想要自定義RatingBar。但它仍然是一個解決方案。

這是可能的。你只要把五個鏡像資源中的每一個在你的項目,然後,做一個佈局是這樣的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="60dp" 
android:layout_height="match_parent" 
android:background="@android:color/black" 
android:gravity="left" 
android:orientation="vertical" 
android:weightSum="5" > 

<ImageView 
    android:id="@+id/oneStar" 
    android:layout_width="50dp" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:src="@drawable/ic_launcher" /> 

<ImageView 
    android:id="@+id/twoStars" 
    android:layout_width="50dp" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:src="@drawable/ic_launcher" 
    android:visibility="visible" /> 

<ImageView 
    android:id="@+id/threeStars" 
    android:layout_width="50dp" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:src="@drawable/ic_launcher" 
    android:visibility="visible" /> 

<ImageView 
    android:id="@+id/fourStars" 
    android:layout_width="50dp" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:src="@drawable/ic_launcher" 
    android:visibility="visible" /> 

<ImageView 
    android:id="@+id/fiveStars" 
    android:layout_width="50dp" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:src="@drawable/ic_launcher" 
    android:visibility="visible" /> 

把你的資源作爲ImageViews的來源。這應該是你的開始:D

+0

我一直在想,但我也希望可以根據我的需要定製ratingbar。無論如何,謝謝你。 – TGIO

+0

這不是正確的方法。你應該使用'RatingBar' –

1

偉大的教程here

我會複製這個答案中的重要部分,以防鏈接無效。

首先,你應該創建它擴展了原有的RatingBar

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="foodRatingBar" parent="@android:style/Widget.RatingBar"> 
     <item name="android:progressDrawable">@drawable/food_ratingbar_full</item> 
     <item name="android:minHeight">48dip</item> 
     <item name="android:maxHeight">48dip</item> 
    </style> 
</resources> 

然後,你需要提供3個可繪製風格,爲什麼呢?因爲你應該填寫3個例子:空的,50%和全部。

這個文件是food_ratingbar_full.xml

>這裏是一個充滿等級的例子(餅乾):
<!-- This is the rating bar drawable that is used to 
show a filled cookie. --> 
<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_pressed="true" 
      android:state_window_focused="true" 
      android:drawable="@drawable/cookie" /> 

    <item android:state_focused="true" 
      android:state_window_focused="true" 
      android:drawable="@drawable/cookie" /> 

    <item android:state_selected="true" 
      android:state_window_focused="true" 
      android:drawable="@drawable/cookie" /> 

    <item android:drawable="@drawable/cookie" /> 

</selector> 

我只是用一個圖像的所有狀態(它實際上看起來像樣), 但是從選擇器中可以看到,有四個di不同的州 可能(@ drawable/cookie終於是一個真正的cookie png圖像)。 這裏很酷的事情是,RatingBar組件會自動根據需要在「完整」和 「空」圖像(如果您支持一半評級,如我的示例圖像中)時填寫部分Cookie。

然後使用你的風格,你應該在RatingBar XML中添加樣式屬性。

style="@style/foodRatingBar 

問題是:如果需要,應該創建一個自定義樣式。您可以使用setRotation來旋轉它。

設置視圖圍繞樞軸點旋轉的角度。增加值會導致順時針旋轉。

+0

我已經讀過這個tut,但它不允許我使用1/5系統。因爲它只能使用1或2張圖片,其中1張爲全屏,2張爲第二張。但我需要使用1/5系統,例如3個5等等 – TGIO