<?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这些东西,但是,我觉它们没有什么可讲的,为什么呢?只要上网搜一下,你就能得到有关于它们的更多详细信息,然而,这里的重点并不是讲这些东西到底是什么,而是告诉大家,怎样做,所以,根本就不需要费尽心思去向别人讲一些其他东西,有关这些东西,我会放在其他地方讲的。也希望我的这篇文章能够对某些人有用,因为正如我前面所讲的,我写博客,单纯只是给自己看,整理自己所想的,所做过的,所以并不会过多的去考虑读者能够看懂或者要尽可能的详细,相信大家也不会对我这个新手抱有太大的期望吧。