[英]Ratingbar component display more than specified number of stars in android
[英]Android RatingBar showing more than 5 stars
我想在我的Android應用程序中通過警告對話框顯示評級欄。 我面臨的問題是,根據屏幕的寬度,評級欄在橫向模式下顯示超過5星(最多10個),而函數setNumStars()無效。 有些帖子已經處理過這個問題,但它們處理的是一個評級欄,其中laout是靜態定義的,而我是動態創建它的。 如何解決這個問題呢?
public class MainActivity extends Activity {
private Button launchButton;
//Rating dialog
private AlertDialog.Builder rater;
private RatingBar ratingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
launchButton =(Button)findViewById(R.id.ratingButton);
launchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Here the alert dialog will be launched
showRatingDialog();
}
});
//Setting up rating dialog
rater = new AlertDialog.Builder(this);
rater.setTitle("This is the rating dialog");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void showRatingDialog()
{
ratingBar = (RatingBar)findViewById(R.id.ratingStars);
rater.setNegativeButton("Abbrechen", null);
rater.setView(ratingBar);
rater.show();
}
}
我已嘗試使用靜態布局進行評級欄,但是當我這樣做並嘗試通過警告對話框顯示評級欄時,不會顯示任何星標。 這是評級欄的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RatingBar
android:id="@+id/ratingStars"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1"
/>
</LinearLayout>
將RatingBar的寬度設置為“換行內容”可以解決整個問題。 根據文件:
設置要顯示的星數。 為了正確顯示這些內容,建議將此窗口小部件的布局寬度設置為包裝內容。
設置android:numStars="5"
和android:layout_width="wrap_content"
嘗試為您的RatingBar
設置最大值,如下所示
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/active_bg" >
<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" />
</RelativeLayout>
在布局文件中創建一個xml文件,並將其設置為內容
像這樣更改showDialog
函數
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.***your_xml_name***));
editor.setView(layout);
editor.show();
保持評級欄的父級為線性布局,並保留該線性布局中的其他內容,這對我有用
這篇博客展示了清晰的解決方案
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.