- 浏览: 314966 次
- 性别:
- 来自: 益阳
文章分类
最新评论
-
duckbit:
楼主是否能把共享layout的例子发给我,有点没明白 谢谢额! ...
Android换肤apk -
天涯海角262253:
...
Androidpn里的Xmpp的理解 -
lbstudy:
Activity之间的切换动画 -
dumbnesslys:
楼主可不可以给个源码 ,就build.xml的 772774 ...
Ant自动打包 -
finaljava:
build.xml 这么复杂,看看这个吧http://angr ...
Ant自动打包
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:
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:
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):
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>:
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:
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!
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!
发表评论
-
浅析QQGame
2012-03-01 14:31 3529通过分析QQGame的项目,发现其存在两种方式: 1. 不安 ... -
opengl初探
2012-02-22 10:15 0android里的surfaceview Surfac ... -
onSaveInstanceState(Bundle outState)的调用时机
2012-02-15 11:10 3150Activity的方法onSaveInstanceState( ... -
Activity之间的切换动画
2012-02-15 10:53 22928从android系统2.1以后,android新增了方法:ov ... -
FLAG_ACTIVITY_NEW_TASK和affinity亲和力
2012-02-06 14:07 3616一直以为在intent里加了FLAG_ACTIVITY_N ... -
什么时候调用Dialog的dismiss()方法
2012-02-03 16:27 10068调用Dialog的dismiss()方法的方式: 1. 重写 ... -
引用主题属性
2011-12-28 18:09 2129文档里写的引用主题属性的方法如下: ?[<pac ... -
Intent的FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_REORDER_TO_FRONT
2011-12-18 14:50 54809Activity的两种启动模式:FLAG_ACTIVITY_C ... -
Android换肤apk
2011-12-11 15:30 4208在android系统中,每 ... -
Androidpn的框架浅析
2011-12-11 13:47 11784开发部署方法: 1. ... -
Androidpn里的Xmpp的理解
2011-12-09 20:31 15064XMPP(可扩展通讯和表示协议)是基于可扩展标记语言(XM ... -
Log的tag的设置
2011-12-09 15:27 2457android输出日志的方法如下: int andro ... -
PopupWindow自适应布局
2011-12-04 18:24 13180Android自带的Menu菜单,常常无法满足我们的需求 ... -
Android程序换肤
2011-12-02 15:15 0Android的换肤功能,有多种方式,现在来说一下生成皮肤ap ... -
Activity与Service通信
2011-12-02 14:46 13142Activity与Service通信的方式有三种: 继 ... -
Ant自动打包2(打特殊厂商的包)
2011-11-29 15:08 4927由于公司内置的需要,我们的程序要针对不同的厂商打不同的ap ... -
Eclipse Indigo设置Courier New字体
2011-11-25 14:25 2192网上的教程如下: ... -
Fragment研究2
2011-11-24 10:43 6530几个类的结构的研 ... -
Ant自动打包
2011-08-23 15:56 39023Ant使用 在ant的官网http://ant. ... -
渐进式下载和流式下载有什么区别
2010-10-23 12:06 2330流式下载 下载边播放的BT软件,下载时必须要从电影的开头下 ...
相关推荐
标题中的"ratingbar"指的是Android开发中的一个关键组件,即评分条。在Android系统中,RatingBar是一个可以显示用户对某项内容进行评级的视图,通常用于电影评分、应用评分或者商品评价等场景。它允许用户通过点击...
可以动态改变星星颜色的RatingBar的Demo,带代码注释。比如当少于三颗星的时候,Ratingbar的星星为红色,少于五颗星的时候,RatingBar的星星颜色为蓝色,五颗星的时候,RatingBar的星星颜色为黄色。
在Android开发中,RatingBar是一个常用的UI组件,用于显示用户评价或者评分的星星。它默认提供了简单的五星级显示,但往往不能满足所有设计需求。这篇博客"RatingBar防错位源码"深入探讨了如何自定义RatingBar,避免...
在Android开发中, RatingBar 是一个非常常见的组件,用于用户对某一内容进行评分。系统默认的RatingBar虽然功能基础,但有时无法满足我们对于界面设计的个性化需求。本篇文章将详细探讨如何根据实际需求自定义...
在Android开发中,RatingBar是一个常用的UI组件,用于展示用户评价或者打分的界面元素。本教程将深入探讨如何自定义一个带有注释的RatingBar,以满足个性化需求。我们将从以下几个方面详细介绍这个过程: 1. **...
在Android开发中,RecyclerView是一个非常重要的组件,它用于展示可滚动的列表数据,而RatingBar则是用户界面中用于评分或反馈满意度的一个控件。在本项目中,我们探讨的是如何在RecyclerView中集成并实现RatingBar...
在Android开发中,RatingBar是用于展示评分的控件,通常用于电影评分、商品评价等场景。然而,开发者在实际应用中可能会遇到RatingBar显示不全或者星星图片被拉伸的问题,这通常与屏幕分辨率、Density DPI(密度独立...
在Android开发中,RatingBar是一种常用的UI组件,用于展示用户评价或者进度的星星数量。默认情况下,RatingBar的样式和颜色可能无法满足所有设计需求,因此开发者常常需要对其进行自定义,以达到更个性化的视觉效果...
在Android开发中,RatingBar是一种常用的UI组件,用于显示用户评价或者打分的条形视图。本教程将深入探讨如何实现一个自定义的RatingBar,以满足特定的设计需求,如设置步长(stepSize)、自定义背景图片以及选择...
在Android开发中,RatingBar是一种常用的视图组件,它用于显示用户评价或评分,通常用于电影、产品或服务的评价场景。本实例将深入探讨如何在Android应用中创建和使用RatingBar,适合初级到高级的Android开发者学习...
在Android开发中,RatingBar是一种常用的UI组件,用于显示用户评价或者评分的星星数量。然而,系统默认的RatingBar可能无法满足所有设计需求,比如样式、颜色、尺寸等。这时,开发者就需要创建自定义的RatingBar来...
Android 中自定义 RatingBar 方法 在 Android 开发中,RatingBar 是一个非常常用的组件,用于显示评分或星级評價。但是,系统默认的 RatingBar 样式可能不够美观,于是我们需要自定义 RatingBar 的样式来满足我们的...
在Android开发中,RatingBar是一种常用的UI组件,用于展示用户评价或者等级评分,通常以星形图标表示。然而,系统默认的RatingBar样式可能无法满足所有设计需求,因此我们需要自定义RatingBar以达到特定的视觉效果。...
而RatingBar则是一个可以显示用户评价星级的控件,通常用于电影、商品等的评分功能。TextView则是用来显示文本信息的基本组件。当我们需要在ListView的每一项中结合RatingBar和TextView来显示动态评分和相关信息时,...
在Android开发中,RatingBar是一种常用的UI组件,它允许用户以星星的形式给出评分。系统默认的RatingBar样式可能无法满足所有设计需求,因此开发者经常需要对其进行自定义以达到更美观的效果。本文将深入探讨如何...
在Android开发中,RatingBar是一个常用的组件,它用于显示评分或者评价,通常表现为星星图标。在本项目中,我们不仅会探讨如何修改RatingBar的基本样式,还会深入学习如何结合属性动画来实现一个模拟QQ群男女比例...
在Android开发中,RatingBar是一种常用的UI组件,用于展示用户评价或者打分的场景,比如电影评分、商品评价等。系统默认的RatingBar通常提供五角星图标,但有时我们可能需要更个性化的视觉效果来匹配应用的设计风格...
在Android开发中, RatingBar 是一个内置的UI组件,用于显示评分或评级,通常用于电影、应用或商品评价。然而,系统默认的RatingBar样式可能无法满足所有设计需求,这时就需要进行自定义来达到特定的视觉效果。这篇...
在Android开发中, RatingBar 是一个内置的控件,用于显示评分或评级,通常以星形图标表示。然而,原生的RatingBar样式有限,不能满足所有设计需求。因此,开发者有时会选择自定义RatingBar来实现更个性化的视觉效果...