带有NumberPicker的AlertDialog呈现不正确 [英] AlertDialog with NumberPicker rendered incorrectly
问题描述
我在AlertDialog中使用NumberPicker进行渲染"时遇到问题.
我给你看代码:
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"
您应该使用
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.
推荐答案
对不起,我之前误会了.您只需要创建一个父视图,然后使用wrap_content将选择器放入其中即可.这是您在代码中的操作方式:
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);
这篇关于带有NumberPicker的AlertDialog呈现不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!