我不明白为什么Android的RatingBar小部件不能缩放到我在布局中指定的宽度。
[如果我有简单的东西
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".home.view.TempActivity">
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="170dp"
android:layout_height="220dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatRatingBar
android:id="@+id/ratingBar"
style="?attr/ratingBarStyleIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:max="5"
android:numStars="5" />
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
最后一颗星星被切断。当然,我可以使用ratingBarStyleSmall
而不是ratingBarStyleIndicator
,但是它太小了。如何获得它,使其仅缩放到父对象的宽度(在本例中为170dp的CardView)?
您可以更改卡片宽度作为包装内容。
您应该将卡片视图android:layout_width
设置为wrap_content
。
并调整RatingBar
的大小,您可以这样做: