我有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>
答案 0 :(得分:3)
设置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>
参见下面的输出
答案 1 :(得分:2)
没有看到父布局,很难说。看起来你有LinearLayout
的链接,你没有设置orientation="vertical"
(除非你想要一个水平布局)。此外,android:layout_width="0dp"
的设置是奇数,没有layout_weight属性。
我建议:
<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)
如果你给我更多布局(父容器),我可能会提供更多帮助。
答案 2 :(得分:1)
评级栏根据单个星的大小获取UI宽度,因此如果分配到评级栏的空间较小,则显示较小的星星,如果分配栏的空间大于要求,则显示的星号更多。 因此,有两个选项可以解决它,你可以根据5星来修复评级栏的宽度,或者选择5个复选框或图像视图,让你感觉舒适并为它们设置逻辑。