2017-01-22 1 views
0

J'ai le code suivant pour RatingBar.La barre d'évaluation ne montre pas toutes les étoiles

<RatingBar 
    android:layout_height="wrap_content" 
    android:layout_width="0dp" 
    android:rating="4" 
    android:id="@+id/product_rating" 
    android:isIndicator="false" 
    android:numStars="5" 
    android:stepSize="0.0" 
    android:max="5" 
    app:layout_constraintRight_toLeftOf="@+id/guideline4" 
    tools:layout_constraintTop_creator="1" 
    tools:layout_constraintRight_creator="1" 
    android:layout_marginTop="8dp" 
    tools:layout_constraintLeft_creator="1" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    android:layout_marginStart="8dp" 
    android:layout_marginLeft="8dp" 
    android:layout_marginEnd="8dp" 
    android:layout_marginRight="8dp"/> 

Mais même si je l'ai utilisé les attributs suivants pour définir les Etoiles Max et taille de pas et les valeurs max, , j'aurais obtenu cinq étoiles visibles. Mais .... Je obtenir le résultat comme celui-ci:

Enter image description here

Si je prends un coup d'oeil au paysage nexus 10 Je reçois ceci: enter image description here

Qu'est-ce que je fais mal?

Addition: J'utilise cette mise en page à nouveau le même problème

<LinearLayout 
    android:layout_width="0dp" 
    android:orientation="vertical" 
    android:layout_height="wrap_content" 
    android:layout_weight="30"> 
    <RatingBar 
     android:id="@+id/rating_bar_product_stats" 
     android:layout_width="wrap_content" 
     android:layout_gravity="center" 
     android:max="5" 
     android:numStars="5" 
     android:layout_height="wrap_content"/> 
    <TextView 
     android:id="@+id/rating_string_product_stats" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     /> 
</LinearLayout> 

Répondre

2

Set android:layout_width="wrap_content" et non android:layout_width="0dp" Parce que si l'utilisation android:layout_width="0dp" et mis en android:layout_weight="1" il peut montrer plus de 5 étoiles basé sur la largeur de l'appareil.

Mise à jour 1 Selon votre code ci-dessus si j'utilise ce que je reçois tous les 5 étoiles ne

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<LinearLayout 
android:layout_width="0dp" 
android:orientation="vertical" 
android:layout_height="wrap_content" 
android:layout_weight="30"> 
<RatingBar 
    android:id="@+id/rating_bar_product_stats" 
    android:layout_width="wrap_content" 
    android:layout_gravity="center" 
    android:max="5" 
    android:numStars="5" 
    android:layout_height="wrap_content"/> 
<TextView 
    android:text="4.4" 
    android:id="@+id/rating_string_product_stats" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    /> 
</LinearLayout> 
</LinearLayout> 

Voir la sortie ci-dessous

enter image description here

+0

didnt travail forme – erluxman

+0

@LaxmanBhattarai S'il vous plaît, postez le fichier xml de mise en page complète. –

+0

s'il vous plaît voir la modification – erluxman

2

Sans voir la mise en page de parent, il est assez difficile à dire. Il semble lien que vous avez un LinearLayout où vous n'avez pas défini orientation="vertical" (sauf si vous voulez une disposition horizontale). En outre, votre paramètre de android:layout_width="0dp" est impair sans un attribut layout_weight.

je suggère:

<RatingBar 
    android:id="@+id/rating" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:numStars="5" 
    android:stepSize="0.1" 
    android:isIndicator="true" /> 

et dans votre code:

findViewById(R.id.rating).setRating(int) 

Si vous me donner plus de la mise en page (le conteneur parent) je pourrais probablement aider plus.

+0

il n'y a qu'un seul parent et c'est la disposition de contrainte – erluxman

1

barre de classement prend la largeur de l'interface utilisateur selon à la taille d'une seule étoile, donc si l'espace alloué à la barre d'évaluation est inférieur à celui des étoiles inférieures et si l'espace alloué à la barre d'évaluation est plus grand que l'étoile s montré sont plus. Il y a donc 2 options pour le résoudre, soit vous pouvez fixer la largeur de la barre d'évaluation en fonction de 5 étoiles ou prendre 5 cases à cocher ou vue de l'image avec laquelle vous êtes à l'aise et définir votre logique.