the rating bar inside my dialog box is going out of bounds
final AlertDialog.Builder popDialog = new AlertDialog.Builder(ExercisesActivity.this);
final RatingBar rb = new RatingBar(ExercisesActivity.this);
popDialog.setIcon(android.R.drawable.btn_star_big_on);
rb.setMax(6);
popDialog.setTitle("Set Difficulty");
popDialog.setView(rb);
// Button OK
popDialog.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//do things
}
});
popDialog.create();
popDialog.show();
setMax()
没有效果
最佳答案
如果您将直接在 AlertDialog
中添加 RatingBar
那么它会导致有线输出,因为我们正在动态生成 Ratingbar
而不是设置宽度.
您可以通过将 LayoutParams
设置为您的 RatingBar
并将您的 RatingBar
添加到 LinearLayout
中并添加LinearLayout
View 到 AlertDialog
。
检查以下示例:
final AlertDialog.Builder popDialog = new AlertDialog.Builder(getActivity());
popDialog.setTitle("Set Difficulty");
popDialog.setIcon(android.R.drawable.btn_star_big_on);
LinearLayout linearLayout = new LinearLayout(ExercisesActivity.this);
LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
final RatingBar rating = new RatingBar(ExercisesActivity.this);
rating.setLayoutParams(lParams);
rating.setNumStars(6);
linearLayout.addView(rating);
popDialog.setView(linearLayout);
// Button OK
popDialog.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//do things
}
});
popDialog.create();
popDialog.show();
输出:
关于Android 评分栏超出对话框范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55701386/