Pretty RatingBar

Android is a great development platform. Numerous built-in components and widgets simplify developer’s life greatly, and Intents are just awesome — in fact, I added sharing of content through Facebook, email and SMS to my application literally with 10 lines of code. Coming from a Java Swing background, I was surprised at how fast I could build pretty complex and functional UIs without writing a bunch of custom components.

However, while there is a plethora of built-in components, the default look and feel of them sometimes leaves much to be desired. Take RatingBar, for example:

[img]http://kozyr.zydako.net/wp-content/uploads/2010/05/defaultRatingBar.png[/img]
To me, this just looks ugly. So, when I needed to add a review/rating “feature” to my application, I started looking for a way to make it prettier and more appropriate to my application (which is about food). As a long-time Swing developer, I immediately thought about subclassing RatingBar and overriding some methods (where’s my paintComponent()? ) — but it turned out there’s an easier way.

Android comes with a “styling” support, which is great, but not really documented enough (you can read a detailed post about Android styles and how to use them in general here). Actually, it’s a must — so please do if you want to continue understanding what’s going on. In a nutshell, styles provide a way to change the look-and-feel of different parts of Android components entirely through XML, while keeping the code and functionality intact. So, with just a few extra lines of XML and a couple of images, your RatingBar could become this:

[img]http://kozyr.zydako.net/wp-content/uploads/2010/05/cookieRatingBar.png[/img]
And how do we get there? First, we’ll need a styles.xml file, which describes our custom styles and lives in the values folder:

<?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>

This creates a new custom style called foodRatingBar which extends Widget.RatingBar style, sets its height to 48 pixels and its progressDrawable to food_rating_bar (RatingBar is just an extension of a ProgressBar, and each “star” in the RatingBar is basically just another “tick” — progressDrawable — in the ProgressBar). progressDrawable documentation is rather lacking, and the only way I figured out which element I needed to style was by looking through Android’s source code (which is a great way to learn things, by the way). It also provides an insight on what should go into the food_rating_bar_full Drawable:

<?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/food_ratingbar_full_empty" />

<item android:id="@+android:id/secondaryProgress"

android:drawable="@drawable/food_ratingbar_full_empty" />

<item android:id="@+android:id/progress"

android:drawable="@drawable/food_ratingbar_full_filled" />

</layer-list>

Basically, it lists out different Drawables to use for background (no cookie – food_ratingbar_full_empty) and progress (selected cookie – food_ratingbar_full_filled). These Drawables are selectors which list out the images to be used in different RatingBar selection states. Here’s an example of a filled rating (cookie):

<?xml version="1.0" encoding="utf-8"?>    

<!-- 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>

I just use one image for all states (and it actually looks decent), but as you can see from the selector, there are four different states possible (@drawable/cookie is finally an actuall cookie png image). And the cool thing here is that RatingBar component will automatically fill in part of the cookie when needed based only on “full” and “empty” images (if you support half ratings, as in my example image).

Finally, it’s time to apply the style to the RatingBar, which is the easiest part — we just add a style attribute to the <RatingBar>:

<RatingBar   android:id="@+id/my_rating_bar" 

...

style="@style/foodRatingBar" />

And that’s it, RatingBar transformation is complete!

One last thing to note is that there are three different types of RatingBar — one interactive, and two read-only (small and large). In order to style the read-only ones, you would need to create another custom style that extends from the appropriate read-only style — for example, for a small one, it would look like this:

<style name="foodRatingBarSmall" parent="@android:style/Widget.RatingBar.Small">  

<item name="android:progressDrawable">@drawable/food_ratingbar_small</item>

<item name="android:minHeight">16dip</item>

<item name="android:maxHeight">16dip</item>

</style>

Basically, we’re just extending from a different parent style, and providing different (smaller) images, and in the read-only case, the half “star” image also needs to be provided.

Have Fun!
AndroidRatingBar 控件是一种用户评分控件,可以在应用中显示一组星形图标,用户可以通过点击星形图标来为应用或产品进行评分。RatingBar 控件通常用于应用市场或评估应用的功能中。 RatingBar 控件有以下属性: - android:numStars:指定 RatingBar 中星形图标的数量。 - android:rating:指定 RatingBar 的当前评分值。 - android:stepSize:指定评分的步长,例如设置为 0.5,则评分只能是整数或半数(例如 3.0、3.5、4.0 等)。 - android:isIndicator:指定 RatingBar 是否是只读的,即用户是否可以更改评分值。 RatingBar 控件还可以通过监听器来检测评分值的变化。例如,可以使用 OnRatingBarChangeListener 监听器来在评分值发生更改时执行自定义操作。 以下是 RatingBar 控件的示例代码: ``` <RatingBar android:id="@+id/ratingBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5" android:stepSize="0.5" android:rating="3.0" android:isIndicator="false" /> ``` 在代码中,可以使用 setRating() 方法来动态设置评分值,例如: ``` RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar); ratingBar.setRating(4.5f); ``` 在监听器中,可以使用 getRating() 方法来获取当前的评分值,例如: ``` RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar); ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { Log.d("MyApp", "Rating changed to " + rating); } }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值