Cómo hacer un RatingBar más pequeño?


He añadido una RatingBar en un diseño:

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

Pero el estilo predeterminado para la barra de calificación es demasiado grande.
He intentado cambiarlo añadiendo el estilo android: style="?android:attr/ratingBarStyleSmall"

Pero el resultado es demasiado pequeño y es imposible establecer una tasa con esta propiedad.

¿Cómo podría hacerlo?

Author: ישו אוהב אותך, 2010-05-20

18 answers

El widget RatingBar predeterminado es sorta' lame.

La fuente hace referencia al estilo "?android:attr/ratingBarStyleIndicator" además del "?android:attr/ratingBarStyleSmall" con el que ya estás familiarizado. ratingBarStyleIndicator es un poco más pequeño, pero sigue siendo bastante feo y los comentarios señalan que estos estilos "no admiten interacción".

Probablemente estés mejor rodando el tuyo. Hay una guía decente en http://kozyr.zydako.net/2010/05/23/pretty-ratingbar / mostrando cómo hacer esto. (No lo he hecho yo mismo sin embargo, pero lo intentaremos en un día o así.)

¡Buena suerte!

P. S. Lo siento, iba a publicar un enlace a la fuente para que usted pueda hurgar en, pero soy un nuevo usuario y no puede publicar más de 1 URL. Si se abre paso a través del árbol de fuentes, se encuentra en frameworks/base/core/java/android/widget/RatingBar.java

 100
Author: Farray,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-07-04 03:07:21

Cómo pegar el código aquí ...

Paso-1. Necesitas tus propias estrellas en res/drawable ...

Una estrella llena

Estrella vacía

Paso - 2 En res/drawable necesita ratingstars.xml de la siguiente manera ...

<?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>

Paso-3 En res/values necesita styles.xml de la siguiente manera ...

<?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>

Paso-4 En su diseño ...

<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"    
/>  
 190
Author: Vaibhav Jani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-15 10:29:27

Solo use:

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

Cambie el factor de la escala según su necesidad.

Para escalar sin crear un relleno a la izquierda también agregue

android:transformPivotX="0dp"
 63
Author: Loolooii,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-30 10:55:59

No hay necesidad de añadir un oyente a ?android:attr/ratingBarStyleSmall. Solo tiene que añadir android:isIndicator=false y capturará eventos de clic, por ejemplo,

<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" />
 39
Author: zyamys,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-13 18:14:31

El pequeño implementar por el sistema operativo

<RatingBar
    android:id="@+id/ratingBar"
    style="?android:attr/ratingBarStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    />
 19
Author: Bouchehboun Saad,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-02-05 06:33:26

Encontré una solución más fácil de lo que creo dada por los anteriores y más fácil que rodar la suya. Simplemente creé una pequeña barra de calificación, luego agregué un OnTouchListener. A partir de ahí calculo el ancho del clic y determino el número de estrellas de eso. Después de haber usado esto varias veces, la única peculiaridad que he encontrado es que el dibujo de una pequeña barra de calificación no siempre sale bien en una tabla a menos que la encierre en un LinearLayout (se ve bien en el editor, pero no en el dispositivo). De todos modos, en mi diseño:

            <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>

Y dentro de mi actividad:

    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; 
        } 
    });

Espero que esto ayude a alguien más. Definitivamente más fácil que dibujar uno mismo (aunque he encontrado que también funciona, pero solo quería un uso fácil de las estrellas).

 15
Author: user1676075,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-17 03:31:55

Aplique esto.

<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" />
 13
Author: Azhar Shaikh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-07-30 09:08:12
<RatingBar
    android:id="@+id/rating_bar"
    style="@style/Widget.AppCompat.RatingBar.Small"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
 4
Author: chry,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-17 15:42:56

Utilice Este Código

<RatingBar
    android:id="@+id/ratingBarSmalloverall"
    style="?android:attr/ratingBarStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="30dp"
    android:isIndicator="false"
    android:numStars="5" />
 2
Author: Dharmendra Mishra,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-03 20:42:44
<RatingBar
    android:id="@+id/ratingBar1"
    android:numStars="5"
    android:stepSize=".5"
    android:rating="3.5"
    style="@android:style/Widget.DeviceDefault.RatingBar.Small"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
 2
Author: M D P,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-13 09:15:48

La mejor respuesta para ratingbar pequeño

<RatingBar
                    android:layout_width="wrap_content"
                    style = "?android:attr/ratingBarStyleSmall"
                    android:layout_height="wrap_content" />
 2
Author: Arun Prajapati,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-01 06:02:20

Aunque la respuesta de Farry funciona, para los dispositivos Samsung RatingBar tomó el color azul aleatorio en lugar del definido por mí. Así que use

style="?attr/ratingBarStyleSmall"

En su lugar.

Código completo cómo usarlo:

<android.support.v7.widget.AppCompatRatingBar
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="?attr/ratingBarStyleSmall" // use smaller version of icons
                android:theme="@style/RatingBar"
                android:rating="0"
                tools:rating="5"/>

<style name="RatingBar" parent="Theme.AppCompat">
        <item name="colorControlNormal">@color/grey</item>
        <item name="colorControlActivated">@color/yellow</item>
        <item name="android:numStars">5</item>
        <item name="android:stepSize">1</item>
    </style>
 1
Author: murt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-01 13:51:39

Después de mucha investigación, encontré que la mejor solución para reducir el tamaño de las barras de calificación personalizadas es obtener el tamaño del elemento de diseño progress en ciertos tamaños como se menciona a continuación:

Xxhdpi-48*48 px

Xhdpi-36*36 px

Hdpi-24 * 24 px

Y en estilo poner el minheight y maxheight como 16 dp para toda la resolución.

Hágamelo saber si esto no le ayuda a obtener un mejor tamaño pequeño barras de clasificación como estos tamaños I encontrado muy compatible y equivalente a ratingbar.pequeño atributo de estilo.

 0
Author: praveen dewangan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-20 18:59:27

Utilice el siguiente código para la barra de clasificación pequeña con el evento onTouch

enter code here


<RatingBar
            android:id="@+id/ratingBar"
            style="?android:ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:isIndicator="false"
            android:rating="4"
            android:stepSize="0.5" />
 0
Author: arunkrishna,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-17 14:13:32

Intente usar alguna biblioteca para la barra de Clasificación, por ejemplo la biblioteca Android Material Rating Bar.

1.Añade tus dependencias:

compile 'me.zhanghai.android.materialratingbar:library:1.2.0'

2.Declare estilo personalizado en sus estilos.archivo xml:

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

3.Agrega la barra de calificaciones en tu diseño y aplica este estilo a través de android: theme attribute:

<me.zhanghai.android.materialratingbar.MaterialRatingBar
    android:id="@+id/rating_bar"
    android:layout_width="wrap_content"
    android:layout_height="22dp"
    android:isIndicator="true"
    android:numStars="5"
    android:rating="0"
    android:stepSize="0.1"
    android:theme="@style/RatingBar"/>

Si desea cambiar el tamaño de la barra de clasificación utilizando esta biblioteca, simplemente cambie android:layout_height y android:layout_width a su preferencia.

 0
Author: Monika Bozhinova,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-01 08:32:38

La mejor respuesta que tengo

  <style name="MyRatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:minHeight">15dp</item>
    <item name="android:maxHeight">15dp</item>
    <item name="colorControlNormal">@color/white</item>
    <item name="colorControlActivated">@color/home_add</item>
</style>

Usuario como este

<RatingBar
                style="?android:attr/ratingBarStyleIndicator"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:isIndicator="false"
                android:max="5"
                android:rating="3"
                android:scaleX=".8"
                android:scaleY=".8"
                android:theme="@style/MyRatingBar" />

Aumentar / Disminuir los tamaños usando scaleX y scaleY valor

 0
Author: AMAN SINGH,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-17 10:27:33

Su trabajo para mí en barra de clasificación xml style="?android: attr / ratingBarStyleSmall " añadir this.it funcionará.

 0
Author: S HemaNandhini,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-04 13:29:51

Resuelvo este problema de la siguiente manera: style="?android: attr / ratingBarStyleSmall "

 -4
Author: Hai Rom,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-19 08:48:20