美文网首页
Android自定义RatingBar评分控件

Android自定义RatingBar评分控件

作者: Haraway | 来源:发表于2017-10-16 17:14 被阅读0次

    RatingBar是基于SeekBar(拖动条)和ProgressBar(状态条)的扩展,用星形来显示等级评定!

    <?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/collect">
        </item>
        <item
            android:id="@android:id/secondaryProgress"
            android:drawable="@drawable/collect">
        </item>
        <item
            android:id="@android:id/progress"
            android:drawable="@drawable/collected">
        </item>
    
    </layer-list>
    
    
     <!-- 评分条样式 -->
        <style name="roomRatingBar" parent="@android:style/Widget.RatingBar">
            <item name="android:progressDrawable">@drawable/star_seletor</item>
            <item name="android:minHeight">45dp</item>
            <item name="android:maxHeight">45dp</item>
        </style>
    
        <style name="ratingbar_style" parent="roomRatingBar">
            <item name="android:layout_width">wrap_content</item>
            <item name="android:layout_height">wrap_content</item>
            <item name="android:spacing">@dimen/space_10</item>
            <item name="android:stepSize">1</item>
            <item name="android:numStars">5</item>
        </style>
    

    如上给ratingbar设置了style后星星”流泪“、”有脚“,解决办法如下:

    private int getScroeHeight() {
            int scroeHeight = 0;
            try {
                Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.collect);
                scroeHeight = bmp.getHeight();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return scroeHeight;
        }
    
     private void setRatingBar(final int positon, int scroeHeight, RatingBar mRatingBar) {
            //在需要的地方设置LayoutParams  记得图片只需要初始化一次就可以了。
            if (scroeHeight != 0) {
                LinearLayout.LayoutParams llp = (LinearLayout.LayoutParams) mRatingBar.getLayoutParams();
                llp.width = -2;// 包裹内容
                llp.height = scroeHeight;
                mRatingBar.setLayoutParams(llp);
            }
            mRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
                public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                    scoreArray[positon] = (int) rating;
                }
            });
        }
    

    参考:
    1,Android自定义RatingBar(星级评分控件)

    2,RatingBar流泪、有脚、显示不全问题

    3,彻底解决RatingBar图片拉伸

    相关文章

      网友评论

          本文标题:Android自定义RatingBar评分控件

          本文链接:https://www.haomeiwen.com/subject/qgbpyxtx.html