android rating bar style
来源:互联网 发布:搭接网络计划STS例如 编辑:程序博客网 时间:2024/06/08 19:00
请看原文:http://kozyr.zydako.net/2010/05/23/pretty-ratingbar/
Android is a great development platform. Numerous built-in components and widgets simplify developer’s life greatly, and Intent
s 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:
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:
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 Drawable
s to use for background (no cookie – food_ratingbar_full_empty
) and progress (selected cookie – food_ratingbar_full_filled
). These Drawable
s 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.
- android rating bar style
- 自定义android Rating bar
- Android ApiDemos示例解析(187):Views->Rating Bar
- android process bar 几种style
- 2H03-view-radio-group-rating-bar
- Android学习路线(九)为Action Bar添加Style
- XP style Explorer Bar
- Action Bar添加Style
- 自定Action Bar Style
- [iOS开发]Status Bar Style
- API Demos 2.3 学习笔记 (12)-- Views->Rating Bar
- Android UI开发神兵利器之Android Action Bar Style Generator
- Rating
- Rating
- Internet Explorer Style Information Bar in JavaScript
- iPhone style title bar in BlackBerry
- apple-mobile-web-app-status-bar-style
- Set cef srcoll bar's css style
- 3月7日整理
- 海量数据处理:十道面试题与十个海量数据处理方法总结
- SVN 入门: 将已有文件目录加入svn版本控制
- 3月8日整理
- UNIX I/O
- android rating bar style
- javascript学习6——javascript面向对象(实战) 制作调试日志
- 离线安装 vs2012 update1
- 从程序员到项目经理(一)
- (转)解决SWFUpload在Chrome、Firefox等浏览器下的问题
- jquery学习
- 打理内心的杂草
- 【我当项目经理那些年】管理杂谈—三个火枪手
- C++中list类使用教程