Android 如何制作更小的 RatingBar?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2874537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:41:27  来源:igfitidea点击:

How to make a smaller RatingBar?

androidandroid-widgetandroid-styles

提问by Clem

I've added a RatingBarin a layout:

我在布局中添加了一个RatingBar

<RatingBar 
    android:id="@+id/ratingbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:stepSize="1.0"
    />  

But the default style for the rating bar is too large.
I've try to change it by adding the android style : style="?android:attr/ratingBarStyleSmall"

但是评分栏的默认样式太大了。
我尝试通过添加 android 样式来更改它:style="?android:attr/ratingBarStyleSmall"

But the result is too small and it's impossible to set a rate with this property.

但结果太小,无法使用此属性设置速率。

How could I do?

我怎么办?

采纳答案by Farray

The default RatingBar widget is sorta' lame.

默认的 RatingBar 小部件有点蹩脚。

The source makes reference to style "?android:attr/ratingBarStyleIndicator" in addition to the "?android:attr/ratingBarStyleSmall" that you're already familiar with. ratingBarStyleIndicatoris slightly smaller but it's still pretty ugly and the comments note that these styles "don't support interaction".

?android:attr/ratingBarStyleIndicator除了?android:attr/ratingBarStyleSmall您已经熟悉的“ ”之外,源还引用了样式“ ” 。 ratingBarStyleIndicator稍微小一点,但它仍然很丑,评论指出这些样式“不支持交互”。

You're probably better-off rolling your own. There's a decent-looking guide at http://kozyr.zydako.net/2010/05/23/pretty-ratingbar/showing how to do this. (I haven't done it myself yet, but will be attempting in a day or so.)

你可能最好自己动手。http://kozyr.zydako.net/2010/05/23/pretty-ratingbar/ 上有一份不错的指南,展示了如何执行此操作。(我自己还没有做过,但会在一天左右的时间里尝试。)

Good luck!

祝你好运!

p.s. Sorry, was going to post a link to the source for you to poke around in but I'm a new user and can't post more than 1 URL. If you dig your way through the source tree, it's located at frameworks/base/core/java/android/widget/RatingBar.java

ps 抱歉,本来打算发布一个源链接供您浏览,但我是新用户,不能发布超过 1 个 URL。如果您在源树中挖掘自己的方式,它位于 frameworks/base/core/java/android/widget/RatingBar.java

回答by Vaibhav Jani

How to glue the code given here...

如何粘贴此处给出的代码...

Step-1. You need your own rating stars in res/drawable...

第1步。您需要自己的评分星级res/drawable...

A full star

一颗满星

Empty star

空星

Step-2 In res/drawableyou need ratingstars.xmlas follow ...

步骤 2 在res/drawable您需要ratingstars.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/star_empty" />
    <item android:id="@android:id/secondaryProgress"
          android:drawable="@drawable/star_empty" />
    <item android:id="@android:id/progress"
          android:drawable="@drawable/star" />
</layer-list>

Step-3 In res/valuesyou need styles.xmlas follow ...

步骤 3 在res/values您需要styles.xml如下...

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="foodRatingBar" parent="@android:style/Widget.RatingBar">
        <item name="android:progressDrawable">@drawable/ratingstars</item>
        <item name="android:minHeight">22dip</item>
        <item name="android:maxHeight">22dip</item>
    </style>
</resources>

Step-4 In your layout ...

第 4 步在您的布局中...

<RatingBar 
      android:id="@+id/rtbProductRating"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:numStars="5"
      android:rating="3.5"
      android:isIndicator="false"
      style="@style/foodRatingBar"    
/>  

回答by Loolooii

Just use:

只需使用:

android:scaleX="0.5"
android:scaleY="0.5"

Change the scale factor according to your need.

根据需要更改比例因子。

In order to scale without creating a padding on the left also add

为了在不创建左侧填充的情况下进行缩放,还添加

android:transformPivotX="0dp"

回答by zyamys

There is no need to add a listener to the ?android:attr/ratingBarStyleSmall. Just add android:isIndicator=falseand it will capture click events, e.g.

无需向?android:attr/ratingBarStyleSmall. 只需添加 android:isIndicator=false它就会捕获点击事件,例如

<RatingBar
  android:id="@+id/myRatingBar"
  style="?android:attr/ratingBarStyleSmall"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:numStars="5"
  android:isIndicator="false" />

回答by Bouchehboun Saad

the small one implement by the OS

操作系统的小工具

<RatingBar
    android:id="@+id/ratingBar"
    style="?android:attr/ratingBarStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    />

回答by Mohammed Azharuddin Shaikh

apply this.

应用这个。

<RatingBar android:id="@+id/indicator_ratingbar"
    style="?android:attr/ratingBarStyleIndicator"
    android:layout_marginLeft="5dip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical" />

回答by user1676075

I found an easier solution than I think given by the ones above and easier than rolling your own. I simply created a small rating bar, then added an onTouchListener to it. From there I compute the width of the click and determine the number of stars from that. Having used this several times, the only quirk I've found is that drawing of a small rating bar doesn't always turn out right in a table unless I enclose it in a LinearLayout (looks right in the editor, but not the device). Anyway, in my layout:

我找到了一个比我认为上面给出的更简单的解决方案,并且比滚动你自己的更容易。我只是创建了一个小的评分栏,然后向其中添加了一个 onTouchListener。从那里我计算点击的宽度并从中确定星星的数量。使用过多次后,我发现的唯一怪癖是,除非我将其包含在 LinearLayout 中(在编辑器中看起来正确,但在设备中看起来不正确),否则绘制小评级栏并不总是正确地显示在表格中. 无论如何,在我的布局中:

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
                <RatingBar
                    android:id="@+id/myRatingBar"
                    style="?android:attr/ratingBarStyleSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:numStars="5" />
            </LinearLayout>

and within my activity:

在我的活动中:

    final RatingBar minimumRating = (RatingBar)findViewById(R.id.myRatingBar);
    minimumRating.setOnTouchListener(new OnTouchListener()
    { 
        public boolean onTouch(View view, MotionEvent event)
        { 
            float touchPositionX = event.getX();
            float width = minimumRating.getWidth();
            float starsf = (touchPositionX / width) * 5.0f;
            int stars = (int)starsf + 1;
            minimumRating.setRating(stars);
            return true; 
        } 
    });

I hope this helps someone else. Definitely easier than drawing one's own (although I've found that also works, but I just wanted an easy use of stars).

我希望这对其他人有帮助。绝对比自己画更容易(虽然我发现这也有效,但我只想轻松使用星星)。

回答by chry

<RatingBar
    android:id="@+id/rating_bar"
    style="@style/Widget.AppCompat.RatingBar.Small"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

回答by indrajeet jyoti

 <RatingBar
                    android:rating="3.5"
                    android:stepSize="0.5"
                    android:numStars="5"
                    style = "?android:attr/ratingBarStyleSmall"
                    android:theme="@style/RatingBar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>

// if you want to style 

 <style name="RatingBar" parent="Theme.AppCompat">
        <item name="colorControlNormal">@color/colorPrimary</item>
        <item name="colorControlActivated">@color/colorAccent</item>
    </style>

// add these line for small rating bar

// 为小评级栏添加这些行

style = "?android:attr/ratingBarStyleSmall"

回答by Arun Prajapati

The Best Answer for small ratingbar

小评级栏的最佳答案

<RatingBar
                    android:layout_width="wrap_content"
                    style = "?android:attr/ratingBarStyleSmall"
                    android:layout_height="wrap_content" />