[英]How to make RatingBar to show five stars
我正在遵循如何添加RatingBar
的標准示例。 為了控制星星的數量,我嘗試使用android:numStars="5"
。 問題是星星的數量似乎根本沒有任何作用。 在縱向布局中,我得到 6 顆星,當我翻轉手機時,我得到大約 10 顆星。 我嘗試在加載 xml 的 Activity ( myBar.setNumStars(5)
) 中設置星數,但該選項也沒有成功。
所以我的問題是如何定義我的布局,使其只顯示五顆星? 設置numStars
似乎不起作用。
提前致謝,羅蘭
<RatingBar
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
android:numStars="5"
android:stepSize="0.1"
android:isIndicator="true" />
在代碼中
mRatingBar.setRating(int)
RatingBar.setNumStar
文檔說:
設置要顯示的星數。 為了正確顯示這些內容,建議此小部件的布局寬度為環繞內容。
所以將布局寬度設置為wrap_content
應該可以解決這個問題。
這對我RatingBar
: RatingBar
應該在LinearLayout
而不是將布局寬度設置為包裝RatingBar
內容。
此外,如果您設置layout_weight
,這將取代numStars
屬性。
您應該只在 XML 中使用numStars="5"
並設置android:layout_width="wrap_content"
。
然后,您可以嘗試使用樣式和其他東西,但是 layout_width 中的“wrap_content”才是訣竅。
如果您正在使用
android:layout_width="match_parent"
使用 wrap_content 它將只顯示設置的 numStars :)
android:layout_width="wrap_content"
<RatingBar
android:id="@+id/ratingBar"
style="@style/custom_rating_bar"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:clickable="true"
android:numStars="5"
/>
配置 XML 文件中的星星數...無需在樣式或活動/片段中提供它.... 重要提示:確保將 WIDTH 作為 Wrap 內容和權重未啟用
我還面臨着超過星星數而不是指定星星數的問題。 為此,我們不想擔心布局類型是相對布局還是線性布局。 簡單地使用寬度如下:
ratingBar.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
請避免將match_parent 和 fill_parent用於ratingbar 。
希望這些東西會有所幫助。
即使我遇到了@Roland 的問題,我也包含了一個名為
android:layout_alignParentRight="true"
在 XML 格式的RatingBar
聲明中。 此屬性阻止設置所需的星星和設置NumStars
隨時發布您遇到的問題!
對我來說,在刪除填充之前我遇到了一個問題。 看起來某些設備上存在一個錯誤,它不會改變視圖的大小以適應填充,而是壓縮內容。
刪除padding
,改用layout_margin
。
要在圓形圖中顯示簡單的星級,只需使用此代碼
public static String getIntToStar(int starCount) {
String fillStar = "\u2605";
String blankStar = "\u2606";
String star = "";
for (int i = 0; i < starCount; i++) {
star = star.concat(" " + fillStar);
}
for (int j = (5 - starCount); j > 0; j--) {
star = star.concat(" " + blankStar);
}
return star;
}
並像這樣使用它
button.setText(getIntToStar(4));
您可以在 xml 布局旁邊添加五顆星的默認評級
android:rating="5"
編輯:
<RatingBar
android:id="@+id/rb_vvm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="@dimen/space2"
android:layout_marginTop="@dimen/space1"
style="@style/RatingBar"
android:numStars="5"
android:stepSize="1"
android:rating="5" />
如果您將RatingBar
包裝在ConstraintLayout
,其寬度為match_constraint
,則編輯器預覽將顯示與其實際寬度成正比的星數,無論您是否設置android:numStars
屬性。 使用wrap_content
獲得正確的預覽:
<RatingBar android:id="@+id/myRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:layout_marginEnd="48dp"
android:numStars="5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RatingBar
android:id="@+id/ruleRatingBar"
android:isIndicator="true"
android:numStars="5"
android:stepSize="0.5"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
我發現RatingBar
拉伸到最大數量的星星,因為它被包含在一個帶有android:stretchColumns = "*"
屬性的表中。
一旦我從所有列中取出了stretchCoulumns
, RatingBar
根據android:numStars
值顯示
View android.view.LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)
以下參數是所有屬性正常工作所必需的:
root
設置parent
attachToRoot
為false
例如: convertView = inflater.inflate(R.layout.myId, parent, false);
默認值在 xml 布局中使用andoid:rating設置。
這里的實際情況是使用wrap_content
而不是match_parent
。 如果您將使用match_parent
布局將填充星星,即使它多於numStars變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.