评分栏未显示所有星星
Rating bar not showing all stars
我有以下 RatingBar
的代码。
<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"/>
但是即使我使用了以下属性来设置最大星数、步长和最大值,我应该有五颗可见的星。但是.... 我得到这样的结果:
如果我看一下 nexus 10 landscape,我会得到以下信息:
我做错了什么?
补充:我再次使用这个布局同样的问题
<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>
在没有看到父布局的情况下很难说。看起来 link 你有一个 LinearLayout
而你还没有设置 orientation="vertical"
(除非你想要水平布局)。如果没有 layout_weight 属性,您对 android:layout_width="0dp"
的设置也很奇怪。
我建议:
<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" />
并在您的代码中:
findViewById(R.id.rating).setRating(int)
如果你给我更多的布局(父容器),我可能会提供更多帮助。
设置 android:layout_width="wrap_content"
而不是 android:layout_width="0dp"
因为如果使用 android:layout_width="0dp"
并设置 android:layout_weight="1"
它可能会根据设备宽度显示超过 5 颗星。
更新 1
根据您上面的代码,如果我使用它,我只会得到所有 5 颗星
<?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>
查看下面的输出
评分栏根据单个星星的大小采用 UI 宽度,因此如果分配给评分栏的 space 较少,则显示较少的星星,如果分配给评分 space酒吧超过要求然后显示的星星更多。
因此,有 2 个选项可以解决它,您可以根据 5 颗星固定评级栏的宽度,或者选择 5 个您喜欢的复选框或图像视图,并为它们设置您的逻辑。
我有以下 RatingBar
的代码。
<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"/>
但是即使我使用了以下属性来设置最大星数、步长和最大值,我应该有五颗可见的星。但是.... 我得到这样的结果:
如果我看一下 nexus 10 landscape,我会得到以下信息:
我做错了什么?
补充:我再次使用这个布局同样的问题
<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>
在没有看到父布局的情况下很难说。看起来 link 你有一个 LinearLayout
而你还没有设置 orientation="vertical"
(除非你想要水平布局)。如果没有 layout_weight 属性,您对 android:layout_width="0dp"
的设置也很奇怪。
我建议:
<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" />
并在您的代码中:
findViewById(R.id.rating).setRating(int)
如果你给我更多的布局(父容器),我可能会提供更多帮助。
设置 android:layout_width="wrap_content"
而不是 android:layout_width="0dp"
因为如果使用 android:layout_width="0dp"
并设置 android:layout_weight="1"
它可能会根据设备宽度显示超过 5 颗星。
更新 1 根据您上面的代码,如果我使用它,我只会得到所有 5 颗星
<?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>
查看下面的输出
评分栏根据单个星星的大小采用 UI 宽度,因此如果分配给评分栏的 space 较少,则显示较少的星星,如果分配给评分 space酒吧超过要求然后显示的星星更多。 因此,有 2 个选项可以解决它,您可以根据 5 颗星固定评级栏的宽度,或者选择 5 个您喜欢的复选框或图像视图,并为它们设置您的逻辑。