带自定义图像的评级栏

时间:2014-03-30 17:56:26

标签: android android-view android-custom-view

是否可以创建这样的自定义评级栏?   如果是的话,你能给我一个如何做的例子吗?

enter image description here

提前致谢!

2 个答案:

答案 0 :(得分:1)

很棒的教程here

如果链接无效,我将在此答案中复制重要部分。

首先,您应该创建一个扩展原始RatingBar

的样式
<?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>

然后你需要提供3个drawable,为什么?因为你应该填写3个案例:空,50%和满。

此文件为food_ratingbar_full.xml

               &GT;以下是填充评级(cookie)的示例:     
<!-- 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>
  

我只使用一个图像用于所有状态(它实际上看起来不错),   但正如您从选择器中看到的,有四种不同的状态   可能(@ drawable / cookie最终是一个实际的cookie png图像)。   这里很酷的是,RatingBar组件会自动生成   在需要时仅根据“完整”填写部分cookie   “空”图像(如果你支持一半的评级,如我的示例图像)。

然后,要使用您的样式,您只需在RatingBar XML中添加样式属性。

style="@style/foodRatingBar

关键是:如果需要,您应该创建一个自定义样式。


然后您可以使用setRotation来旋转它。

  

设置视图围绕轴心点旋转的度数。增加值会导致顺时针旋转。

答案 1 :(得分:-5)

编辑:这不是一个合适的答案,因为OP想要自定义RatingBar。但它仍然是一种解决方案。

有可能。您只需将五个图像资源中的每一个放入项目中,然后制作如下布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="@android:color/black"
android:gravity="left"
android:orientation="vertical"
android:weightSum="5" >

<ImageView
    android:id="@+id/oneStar"
    android:layout_width="50dp"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:src="@drawable/ic_launcher" />

<ImageView
    android:id="@+id/twoStars"
    android:layout_width="50dp"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:src="@drawable/ic_launcher"
    android:visibility="visible" />

<ImageView
    android:id="@+id/threeStars"
    android:layout_width="50dp"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:src="@drawable/ic_launcher"
    android:visibility="visible" />

<ImageView
    android:id="@+id/fourStars"
    android:layout_width="50dp"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:src="@drawable/ic_launcher"
    android:visibility="visible" />

<ImageView
    android:id="@+id/fiveStars"
    android:layout_width="50dp"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:src="@drawable/ic_launcher"
    android:visibility="visible" />

将您的资源作为ImageViews的来源。这应该是你的开始:D

相关问题