android自定义评分条(RatingBar)

##android自定义评分条(RatingBar)

  • 本自定义控件是因为本人在开发过程中,评分条的适配出现问题而诞生,在网上找了些自定义控件,发现都不是自己想要的,所以只能自己动手。

  • 原生的RatingBar适配难是因为在要自定义星星图标无法设置星星的大小和星星之间的间距,适配了这个厂商的手机可能那个手机有又问题,当时我就是在vivo手机适配好,到了同样分辨率的乐视手机出现了问题,我这里的思路是:

  • 直接用一个imageView来表示一颗星星,用一个线性布局包裹所有的imageview,为了以后实现非整数颗星的显示,所以不能用图片来显示。这里的方式是用一个相对布局包裹两个Imageview表示一颗星星,前面ImageView设置图片为空的星星,后面的ImageView用来填充星星的颜色,用填充的部分代表评分,如3.5分就是用三颗全部填充的星星可一颗填充一半的星星表示,用设置后面的ImageView的布局参数来实现填充

  • 因为公司需求不需要非整数颗星的评分,所以这里只做了整颗星的评分,可以根据需要修改里面的代码。

  • 控件特点:
    1、随意设置星星之间的间隔
    2、只需一张空的星星图片,减少图片带来的内存占用
    3、可以显示0~5任意数值的星级
    4、不用担心不同分辨率、不同厂商带来的屏幕适配问题

  • 限制:图片的星星以外的颜色必须要和评分条的背景颜色一致,不然的话那效果……

  • 效果:这里写图片描述
    这里写图片描述
    自定义RatingBar的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="wrap_content"
              android:id="@+id/llt_root"
              android:layout_height="match_parent">
    <RelativeLayout
        android:id="@+id/star_container01"
        android:layout_width="0px"
        android:layout_height="0px">
        <ImageView
            android:id="@+id/star01"
            android:background="@color/star_color"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
        <ImageView
            android:id="@+id/star_border01"
            android:layout_width="match_parent"
            android:background="@drawable/ic_star_border"
            android:layout_height="match_parent"/>
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/star_container02"
        android:layout_width="0px"
        android:layout_marginLeft="5dp"
        android:layout_height="0px">
        <ImageView
            android:id="@+id/star02"
            android:background="@color/star_color"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
        <ImageView
            android:id="@+id/star_border02"
            android:layout_width="match_parent"
            android:background="@drawable/ic_star_border"
            android:layout_height="match_parent"/>
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/star_container03"
        android:layout_width="0px"
        android:layout_marginLeft="5dp"
        android:layout_height="0px">
        <ImageView
            android:id="@+id/star03"
            android:background="@color/star_color"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
        <ImageView
            android:id="@+id/star_border03"
            android:layout_width="match_parent"
            android:background="@drawable/ic_star_border"
            android:layout_height="match_parent"/>
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/star_container04"
        android:layout_width="0px"
        android:layout_marginLeft="5dp"
        android:layout_height="0px">
        <ImageView
            android:id="@+id/star04"
            android:background="@color/star_color"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
        <ImageView
            android:id="@+id/star_border04"
            android:layout_width="match_parent"
            android:background="@drawable/ic_star_border"
            android:layout_height="match_parent"/>
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/star_container05"
        android:layout_width="0px"
        android:layout_marginLeft="5dp"
        android:layout_height="0px">
        <ImageView
            android:id="@+id/star05"
            android:background="@color/star_color"
            android:layout_width
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
AndroidRatingBar 控件是一种用户评分控件,可以在应用中显示一组星形图标,用户可以通过点击星形图标来为应用或产品进行评分RatingBar 控件通常用于应用市场或评估应用的功能中。 RatingBar 控件有以下属性: - android:numStars:指定 RatingBar 中星形图标的数量。 - android:rating:指定 RatingBar 的当前评分值。 - android:stepSize:指定评分的步长,例如设置为 0.5,则评分只能是整数或半数(例如 3.0、3.5、4.0 等)。 - android:isIndicator:指定 RatingBar 是否是只读的,即用户是否可以更改评分值。 RatingBar 控件还可以通过监听器来检测评分值的变化。例如,可以使用 OnRatingBarChangeListener 监听器来在评分值发生更改时执行自定义操作。 以下是 RatingBar 控件的示例代码: ``` <RatingBar android:id="@+id/ratingBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5" android:stepSize="0.5" android:rating="3.0" android:isIndicator="false" /> ``` 在代码中,可以使用 setRating() 方法来动态设置评分值,例如: ``` RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar); ratingBar.setRating(4.5f); ``` 在监听器中,可以使用 getRating() 方法来获取当前的评分值,例如: ``` RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar); ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { Log.d("MyApp", "Rating changed to " + rating); } }); ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值