Android 评级栏未显示所有星级
我有以下Android 评级栏未显示所有星级,android,ratingbar,Android,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" andro
分级栏的代码
<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景观,我会发现:
我做错了什么
另外:我正在使用这个布局再次相同的问题
<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>
如果看不到父布局,很难说。如果您没有设置
orientation=“vertical”
(除非您想要水平布局),那么它看起来像是一个LinearLayout
。另外,您的android:layout\u width=“0dp”
设置是奇数,没有layout\u 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" />
如果你给我更多的版面(父容器),我可能会帮助更多。设置
android:layout\u width=“wrap\u content”
而不是android:layout\u width=“0dp”
因为如果使用android:layout\u width=“0dp”
并设置android:layout\u 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宽度,因此,如果分配给分级栏的空间较小,则显示较小的星;如果分配给分级栏的空间大于要求,则显示的星较多。
因此,有两个选项可以解决此问题,您可以根据5颗星固定评级栏的宽度,也可以选择5个复选框或图像视图,只要您愿意,并为它们设置逻辑。只有一个父项,这就是约束布局为什么我不能得到5颗星,而只得到2或一半左右
<?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>