`
handsomeliuyang
  • 浏览: 314963 次
  • 性别: Icon_minigender_1
  • 来自: 益阳
社区版块
存档分类
最新评论

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:


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 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!
分享到:
评论

相关推荐

    ratingbar

    标题中的"ratingbar"指的是Android开发中的一个关键组件,即评分条。在Android系统中,RatingBar是一个可以显示用户对某项内容进行评级的视图,通常用于电影评分、应用评分或者商品评价等场景。它允许用户通过点击...

    动态改变RatingBar星星颜色

    可以动态改变星星颜色的RatingBar的Demo,带代码注释。比如当少于三颗星的时候,Ratingbar的星星为红色,少于五颗星的时候,RatingBar的星星颜色为蓝色,五颗星的时候,RatingBar的星星颜色为黄色。

    RatingBar防错位源码

    在Android开发中,RatingBar是一个常用的UI组件,用于显示用户评价或者评分的星星。它默认提供了简单的五星级显示,但往往不能满足所有设计需求。这篇博客"RatingBar防错位源码"深入探讨了如何自定义RatingBar,避免...

    Android自定义Ratingbar星星实现评分

    在Android开发中, RatingBar 是一个非常常见的组件,用于用户对某一内容进行评分。系统默认的RatingBar虽然功能基础,但有时无法满足我们对于界面设计的个性化需求。本篇文章将详细探讨如何根据实际需求自定义...

    android定制评分RatingBar带注释

    在Android开发中,RatingBar是一个常用的UI组件,用于展示用户评价或者打分的界面元素。本教程将深入探讨如何自定义一个带有注释的RatingBar,以满足个性化需求。我们将从以下几个方面详细介绍这个过程: 1. **...

    RecyclerView防RatingBar功能

    在Android开发中,RecyclerView是一个非常重要的组件,它用于展示可滚动的列表数据,而RatingBar则是用户界面中用于评分或反馈满意度的一个控件。在本项目中,我们探讨的是如何在RecyclerView中集成并实现RatingBar...

    RatingBar显示不全或图片拉伸解决方案

    在Android开发中,RatingBar是用于展示评分的控件,通常用于电影评分、商品评价等场景。然而,开发者在实际应用中可能会遇到RatingBar显示不全或者星星图片被拉伸的问题,这通常与屏幕分辨率、Density DPI(密度独立...

    Android自定义RatingBar的背景图片

    在Android开发中,RatingBar是一种常用的UI组件,用于展示用户评价或者进度的星星数量。默认情况下,RatingBar的样式和颜色可能无法满足所有设计需求,因此开发者常常需要对其进行自定义,以达到更个性化的视觉效果...

    自定义RatingBar

    在Android开发中,RatingBar是一种常用的UI组件,用于显示用户评价或者打分的条形视图。本教程将深入探讨如何实现一个自定义的RatingBar,以满足特定的设计需求,如设置步长(stepSize)、自定义背景图片以及选择...

    Android RatingBar投票条实例

    在Android开发中,RatingBar是一种常用的视图组件,它用于显示用户评价或评分,通常用于电影、产品或服务的评价场景。本实例将深入探讨如何在Android应用中创建和使用RatingBar,适合初级到高级的Android开发者学习...

    模仿RatingBar

    在Android开发中,RatingBar是一种常用的UI组件,用于显示用户评价或者评分的星星数量。然而,系统默认的RatingBar可能无法满足所有设计需求,比如样式、颜色、尺寸等。这时,开发者就需要创建自定义的RatingBar来...

    android中自定义ratingbar方法(含代码)

    Android 中自定义 RatingBar 方法 在 Android 开发中,RatingBar 是一个非常常用的组件,用于显示评分或星级評價。但是,系统默认的 RatingBar 样式可能不够美观,于是我们需要自定义 RatingBar 的样式来满足我们的...

    自定义ratingBar

    在Android开发中,RatingBar是一种常用的UI组件,用于展示用户评价或者等级评分,通常以星形图标表示。然而,系统默认的RatingBar样式可能无法满足所有设计需求,因此我们需要自定义RatingBar以达到特定的视觉效果。...

    ListView +RatingBar +TextView +adapter

    而RatingBar则是一个可以显示用户评价星级的控件,通常用于电影、商品等的评分功能。TextView则是用来显示文本信息的基本组件。当我们需要在ListView的每一项中结合RatingBar和TextView来显示动态评分和相关信息时,...

    android 自定义RatingBar源码实例

    在Android开发中,RatingBar是一种常用的UI组件,它允许用户以星星的形式给出评分。系统默认的RatingBar样式可能无法满足所有设计需求,因此开发者经常需要对其进行自定义以达到更美观的效果。本文将深入探讨如何...

    RatingBar修改背景

    在Android开发中,RatingBar是一个常用的组件,它用于显示评分或者评价,通常表现为星星图标。在本项目中,我们不仅会探讨如何修改RatingBar的基本样式,还会深入学习如何结合属性动画来实现一个模拟QQ群男女比例...

    自定义的RatingBar

    在Android开发中,RatingBar是一种常用的UI组件,用于展示用户评价或者打分的场景,比如电影评分、商品评价等。系统默认的RatingBar通常提供五角星图标,但有时我们可能需要更个性化的视觉效果来匹配应用的设计风格...

    android自定义RatingBar

    在Android开发中, RatingBar 是一个内置的UI组件,用于显示评分或评级,通常用于电影、应用或商品评价。然而,系统默认的RatingBar样式可能无法满足所有设计需求,这时就需要进行自定义来达到特定的视觉效果。这篇...

    android自定义评分控件ratingbar

    在Android开发中, RatingBar 是一个内置的控件,用于显示评分或评级,通常以星形图标表示。然而,原生的RatingBar样式有限,不能满足所有设计需求。因此,开发者有时会选择自定义RatingBar来实现更个性化的视觉效果...

Global site tag (gtag.js) - Google Analytics