18

Androidアプリのアラートダイアログで評価バーを表示したい。私が直面している問題は、画面の幅によっては、評価バーに横向きモードで5つ以上の星(最大10個)が表示され、関数setNumStars()が効果を発揮しないことです。すでにこの問題を扱っている投稿がありますが、動的に作成している間、ラウトが静的に定義されているレーティングバーを扱っています。この問題を解決する方法は?

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>
4

5 に答える 5

21

とを設定しandroid:numStars="5"ますandroid:layout_width="wrap_content"

于 2014-05-28T11:24:03.407 に答える
19

RatingBarの幅を「コンテンツをラップ」に設定すると、問題全体が解決します。ドキュメントによると:

表示する星の数を設定します。これらを正しく表示するには、このウィジェットのレイアウト幅をラップコンテンツにすることをお勧めします。

于 2013-07-02T18:51:01.633 に答える
11

RatingBarLikeBelowの最大値を設定してみてください

<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();
于 2013-03-19T16:21:15.067 に答える
3

評価バーの親を線形レイアウトとして保持し、その線形レイアウトには他に何も保持しないでください。

この ブログは、ソリューションの明確な全体像を示しています

于 2016-09-23T06:20:38.547 に答える
2

RatingBarがAlertDialogにある場合は、LinearLayoutの内部に固定し、LinearLayoutでsetViewを使用します。次に、その幅をWRAP_CONTENTに設定できます。

于 2019-09-20T14:01:04.557 に答える