2013-03-19 73 views
14

我想通過我的Android應用程序中的提示對話框顯示評級欄。我面臨的問題是,根據屏幕的寬度,在橫向模式下,評分欄顯示超過5顆星(最多10顆),而函數setNumStars()不起作用。有些帖子已經處理了這個問題,但他們處理的是一個等級欄,它的靜態定義是我動態創建的。如何解決這個問題呢?Android RatingBar顯示超過5顆星

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(); 

} 
} 

我已經使用了的RatingBar靜態佈局嘗試,但我做到這一點,並試圖通過顯示一個警告對話框中的RatingBar時,沒有星星所示。下面是評價欄佈局文件:

<?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> 
+0

嘗試將步長設置爲0.1 – Triode 2013-03-19 16:15:47

回答

8

試圖爲您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(); 
+1

這些更改不起作用。 – user1107888 2013-03-19 16:43:35

+0

當我在xml文件中聲明並設置其內容工作正常,如果你想我可以發佈代碼 – Triode 2013-03-19 16:47:31

+0

是的,如果它沒有問題,你 – user1107888 2013-03-19 16:49:09

17

se將RatingBar的寬度設置爲「包裝內容」解決了整個問題。 根據文檔:

設置要顯示的恆星數量。爲了正確顯示 ,建議將此小部件的佈局寬度包裝爲 內容。

13

設置了android:numStars =「5」和android:layout_width =「WRAP_CONTENT」評級酒吧線性佈局

0

保持父,並保持閒來無事在線性佈局,這對我

工作

This 博客顯示解決方案的清晰圖片