[英]Android rating bar going out of bounds of dialog
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()
無效
首先指定高度和寬度:
LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
linearLayout.addView(rb);
設置顯示應用setNumStars(x)的星數。 為了正確顯示這些內容,建議將此窗口小部件的布局寬度設置為包裝內容。 喜歡:
rb.setNumStars(6);
您可以像這樣設置自己的寬度:
rb.getLayoutParams().width = WindowManager.LayoutParams.MATCH_PARENT;
// you can also use WRAP_CONTENT but I think that for your dialog MATCH_PARENT is better
setMax()方法定義用戶可以選擇的最大啟動次數t6。
根據您的查詢,我希望您正在尋找設置要顯示的星數。
所以在你的代碼中刪除
rb.setMax(6);
並替換它
rb.setNumStars(6);
希望iv給你一個解決方案:)
如果您將在AlertDialog
直接添加RatingBar
,那么它將導致有線輸出,因為我們動態生成Ratingbar
而不是設置寬度。
您可以通過設置實現相同LayoutParams
到您RatingBar
並添加您RatingBar
到LinearLayout
,並添加LinearLayout
欣賞到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();
輸出:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.