AlertDialog with NumberPicker rendered incorrectly

点点圈 提交于 2019-12-21 05:16:24

问题


I' have a problem about 'rendering' with my NumberPicker inside an AlertDialog.
I show to you the code:

                //setup del dialog
                AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                builder.setCancelable(false);

                final NumberPicker picker = new NumberPicker(activity);
                picker.setMinValue(0);
                picker.setMaxValue(5);
                //set bottoni dialog
                builder.setPositiveButton(R.string.dialog_btn_seleziona, seleziona);
                builder.setNegativeButton(R.string.dialog_btn_annulla, null);

                //visualizzo il dialog
                Dialog dialog = builder.create();
                dialog.show();

the problem is that instead of to have a result like this:



I have this result:


The blue line selector is too long, i would like the same lenght of the first picture
i founded this topic:
NumberPicker rendered incorrectly in a Dialog
that explain how to fix this problem, and the solution is:

instead of this
android:layout_width="match_parent"

You should use

android:layout_width="wrap_content"
android:layout_gravity="center"

the problem is that i dont have a layout (made by myself for this numberpicker) so i need to set this parameters programmatically, but i don't know how.


回答1:


Sorry, I misunderstood earlier. You just need to create a parent view and put your picker inside of that, using wrap_content. Here is how you do that in code:

final NumberPicker picker = new NumberPicker(activity);
picker.setMinValue(0);
picker.setMaxValue(5);
final FrameLayout parent = new FrameLayout(activity);
parent.addView(picker, new FrameLayout.LayoutParams(
        FrameLayout.LayoutParams.WRAP_CONTENT,
        FrameLayout.LayoutParams.WRAP_CONTENT,
        Gravity.CENTER));
builder.setView(parent);


来源:https://stackoverflow.com/questions/27263008/alertdialog-with-numberpicker-rendered-incorrectly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!