<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RatingBar android:id="@+id/ratingBar" android:layout_height="wrap_content" android:layout_width="wrap_content" android:rating = "3.5" android:numStars="5" android:stepSize = "0.5"/> </LinearLayout>
接着就是activity的代碼:
@SuppressWarnings("unused") private RatingBar ratingBar; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
效果圖如下:
這里,就對布局文件上幾個屬性進行說明,因為這是我們構建RatingBar必須要了解的,而且還是我們需要修改的。這里,就要提示各位,如果你的布局中有RatingBar,那么你的layout_width和layout_height最好要設置成wrap_content,至少layout_width是必須這么做的,因為如果沒有這么做,那么很可能你的星星顯示不出來,當然,你可以修改一下星星的大小,就可以顯示更多的星星。那么,接下來,就是介紹RatingBar中的相關屬性。
android:rating = "3.5",這一條是設置默認的分數,可以是浮點數,因為我們的RatingBar其實是評分條,所以星星的數量其實就是用來顯示分數的數量,而默認的分數就是應用一開始出現的星星的數量。
android:numStars="5",這個是設置顯示的星星數量,理論上,是任何整數都可以,但是我的建議就是最好就是5,因為如果多於5的話,我試過,就我的手機,3.7寸大的屏幕,最多只能顯示5.8,為什么是這么怪的數據,因為那個第六個星星根本就還有一角沒有顯示出來。
android:stepSize = "0.5“,這是顯示步長的。什么是步長,就是當你的分數增加時,是按照多大的分數增加的,就像這里,步長是0.5,那么你的手機上顯示的星星是可以用你的手指點擊來增加分數的,而這里就會按照一次點擊增加0.5的步長,就是半顆星星。
但是,這里有點必須要注意,就是我們有時候,不,應該是大部分情況,都不希望我們的RatingBar是可以改動的,是固定的顯示分數的,那么,這時我們應該怎樣做呢?就是再增加一條屬性,就是android:isIndicator="true",這樣分數就會固定,不會改變,所以,步長的設定就完全沒有必要。
當然,上面的某些屬性也是有相應的方法是可以更改的,但是一般情況下都是不需要修改的,如果你的設計需要的話,可以看一下文檔,里面有介紹,那么我這里就不多說了。
上面的代碼已經可以定義一個基本樣式的RatingBar了,所以第一個問題基本上已經解決了,那么,接下來就是第二個問題:
二.RatingBar樣式的修改
很多時候,默認的RatingBar並不能滿足我們的要求,因為我們的應用的需求是各種各樣的,默認的樣式實在是太過於單調了。這時就需要修改RatingBar的屬性了。一般而言,我們都只會修改RatingBar的大小,圖樣,顏色,等等。所以,這里我就只挑幾種比較常見的,就是上面說到的三種。
1.大小。
默認的RatingBar的星星大小,老實說,實在太大了,因為有時候我們呢,會想要將RatingBar放在其他組件上,如ListView,所以,默認的大小肯定不符合我們的要求。那么,我們需要將星星變小一點,於是就需要在RatingBar的布局中添加這么一句:style="?android:attr/ratingBarStyleSmall"這樣,星星的大小就會變小,效果圖如下:
很抱歉,就我目前收集到的資料來看,星星的大小就只有默認和變小這兩種模式,沒有其他的情況,但是就我目前的使用情況來說,已經足夠了,如果你們有其他要求,那么,我的建議就是換掉默認的星星圖案吧。
接下來就是顏色和圖樣的改變。為什么這兩條要放在一塊講呢?因為這兩條的修改就是自定義自己的RatingBar,所謂的自定義,其實就是用自己的圖樣換掉默認的圖案,這樣是最好的情況,因為包括大小,顏色等等你都能使用自己滿意的樣式。因為我實在是沒有找到什么相關的方法和資料能夠解決這個問題,基本上,網上都是選擇替換掉圖案,因為系統中的星星的圖案是固定的,它就只是一張圖片而已,所以應該真的是沒法用代碼進行修改。應該吧?因為以后可能就不一樣了吧。方法如下:
2.RatingBar的自定義設置:
我們還是要在我們的RatingBar的布局中添加這么一句:style="@style/myRatingBar",然后就是開始創建我們的myRatingBar的xml文件了。在res/values目錄下創建一個xml文件,然后下面是代碼:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="MyRatingBar" parent="@android:style/Widget.RatingBar"> <item name="android:progressDrawable">@drawable/myRatingBar</item> <item name="android:minHeight">15dip</item> <item name="android:maxHeight">15dip</item> </style> </resources>
item name="android:minHeight"和item name="android:maxHeight"可以自己定義自己的RatingBar的大小。
接着是在res/drawable下創建我們的myRatingBar.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" /> <item android:id="@+android:id/progress" android:drawable="@drawable/rating_show" /> </layer-list>
這里面,第一個item是設置背景圖案的,第二個item是設置RatingBar圖案的。
到了這里,基本上的設置已經搞定了,我們可以自定義屬於自己的RatingBar了。由於本人手上沒有什么圖片資源,所以,就貼別人的效果圖出來吧,如果有得罪該圖的作者的話,還請見諒啊。
接下來就是RatingBar與其他組件的搭配。這方面,就以我自己的東西為例好了,就是在ListView上顯示RatingBar,這個需求是非常常見的。
三。ListView上顯示RatingBar
對於這個,很多人都會選擇自定義ListView的適配器,但是經過我的查找,發現是可以不用這么麻煩的(當然,自定義也不是很麻煩,如果你要處理的組件很多的話,那么自定義的確是很好的選擇,但是這里我就不講了,因為我這里的前提就是默認大家只是想我一樣,只想在ListView上顯示RatingBar,那么就沒有必要那么麻煩,就像我當初做的一樣)。下面就直接貼出代碼:
public class RatingBarActivity extends Activity { /** Called when the activity is first created. */ private RatingBar ratingBar; private ListView list; private List<Map<String, Object>> data; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ratingBar = (RatingBar) findViewById(R.id.ratingBar); list = (ListView) findViewById(R.id.list); data = getData(); SimpleAdapter adapter = new SimpleAdapter(this, this.data, R.layout.item, new String[] { "book", "rating" }, new int[] { R.id.book, R.id.ratingBar }); adapter.setViewBinder(new SimpleAdapter.ViewBinder() { @Override public boolean setViewValue(View view, Object data, String textRepresentation) { if ((view instanceof RatingBar) && (data instanceof Float)) { ratingBar = (RatingBar) view; ratingBar.setRating((Float) data); return true; } return false; } }); list.setAdapter(adapter); } private List<Map<String, Object>> getData() { Map<String, Object> map = new HashMap<String, Object>(); List<Map<String, Object>> mlist = new ArrayList<Map<String, Object>>(); String book = "安卓應用"; map.put("book", book); map.put("rating", Float.parseFloat(3 + "")); mlist.add(map); return mlist; } }
然后是要有兩個布局文件,一個是main.xml,如:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout>
另一個就是存放布局組件的xml文件,像是RatingBar, TextView,反正就是你想要放在ListView上的組件最好單獨放在一個xml文件中,如:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_margin="5dp"> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:collapseColumns="2" android:stretchColumns="1"> <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/book" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18dp"/> <RatingBar android:id="@+id/ratingBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:paddingTop="5dp" android:numStars="5" style="?android:attr/ratingBarStyleSmall"/> </TableRow> </TableLayout> </LinearLayout> </LinearLayout>
效果圖如:
至此,全文就結束了。當然,我承認,里面有些東西我根本就沒有講清楚,像是最后一個,我只是貼代碼出來,但是我認為那根本就沒啥好講的,因為如果是里面的一些方法,就像ListView的適配器的設置,或者像是TableLayout這些東西,但是,我覺它們沒有什么可講的,為什么呢?只要上網搜一下,你就能得到有關於它們的更多詳細信息,然而,這里的重點並不是講這些東西到底是什么,而是告訴大家,怎樣做,所以,根本就不需要費盡心思去向別人講一些其他東西,有關這些東西,我會放在其他地方講的。也希望我的這篇文章能夠對某些人有用,因為正如我前面所講的,我寫博客,單純只是給自己看,整理自己所想的,所做過的,所以並不會過多的去考慮讀者能夠看懂或者要盡可能的詳細,相信大家也不會對我這個新手抱有太大的期望吧。