Android 评分栏超出对话框范围

标签 android android-layout

the rating bar inside my dialog box is going out of bounds enter image description here

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

输出:

enter image description here

关于Android 评分栏超出对话框范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55701386/

相关文章:

android - 无法在应用程序中从 XML 调用对象

android - 强制在 Android 应用程序中更新证书 .pem

android - 如何分享ImageView的图片?

java - 为什么 ListView 需要将其容器项放在单独的布局文件中

android - 设置Android工具栏导致程序运行失败

android - 设置自定义属性android的值

java - 使用 RoboGuice 2 使用带有参数的构造函数注入(inject)对象

Java 下载 html

android - 为什么 toogleButton 进入默认模式?

android - 在 ViewPager 中获取 Activity 页面并更新内容