繁体   English   中英

对话框中的Android Ratingbar不同屏幕尺寸问题

[英]Android Ratingbar in dialog different screen size issue

我在对话框中有一个评分栏。 我将最大星星数设置为6 以下代码在屏幕尺寸较小的智能手机上可以正常使用。 但是,在平板电脑(10英寸)上进行测试时,我看到11开始并且布局包装了内容。 为什么会发生这种奇怪的行为? 以及如何解决?

public void ShowDialog()
    {
        final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
        final RatingBar rating = new RatingBar(this);
        rating.setMax(6);

        popDialog.setTitle("Vote!! ");
        popDialog.setView(rating);

        // Button OK
        popDialog.setPositiveButton(android.R.string.ok,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        txtView.setText(String.valueOf(rating.getProgress()));
                        dialog.dismiss();
                    }

                })

        // Button Cancel
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });

        popDialog.create();
        popDialog.show();

    }

根据官方文件,

当设置布局宽度以包装内容时,将显示(通过setNumStars(int)或在XML布局中)设置的星形数(如果设置了另一个布局宽度,则结果可能无法预测)。

另外,您应该使用setNumStars()设置星数,而不是setMax()

通过调用setLayoutParams()在等级栏上设置Layout width属性:

        ratingBar.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM