[英]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"/>
但是,即使我使用以下属性来设置“最大星星数”,步长和最大值, 我还是应该有五个可见星星。 但是....我得到这样的结果:
我究竟做错了什么?
另外:我再次使用这种布局存在同样的问题
<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>
设置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>
见下面的输出
没有看到父布局,很难说。 它看起来与您具有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)
如果您给我更多的布局(父容器),我可能会提供更多帮助。
评级栏根据单个星的大小采用UI宽度,因此,如果分配给评级栏的空间较小,则显示的星星较少;如果分配给评级栏的空间大于所需的空间,则显示的星星较多。 因此,有2个解决方案,您可以根据5个星标固定等级栏的宽度,也可以选择5个您喜欢的复选框或图像视图并为其设置逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.