I want to show two number picker using dialog

断了今生、忘了曾经 提交于 2019-12-13 01:34:08

问题


I am trying to show two number picker on dialog using java only, The code is working but i am not able to arrange it on equal width.

here is my code

RelativeLayout relative = new RelativeLayout(mContext);
final NumberPicker aNumberPicker = new NumberPicker(mContext);
aNumberPicker.setMaxValue(50);
aNumberPicker.setMinValue(1);
final NumberPicker aNumberPickerA = new NumberPicker(mContext);
aNumberPickerA.setMaxValue(11);
aNumberPickerA.setMinValue(1);
aNumberPickerA.setDisplayedValues(new String[] { "Tea Cup", "Glass","Plate","Small Plate","Cutlets","Medium","Piece","Katori","Balls","Serving","egg"});

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50, 50);

RelativeLayout.LayoutParams numPicerParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams qPicerParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);


numPicerParams.addRule(RelativeLayout.CENTER_IN_PARENT);
qPicerParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
relative.setLayoutParams(params);
relative.addView(aNumberPicker,numPicerParams);
relative.addView(aNumberPickerA,qPicerParams);


AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
alertDialogBuilder.setTitle("Select the number");
alertDialogBuilder.setView(relative);
alertDialogBuilder
        .setCancelable(false)
        .setPositiveButton("Ok",
            new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,
                    int id) {
                Log.e("","New Quantity Value : "+ aNumberPicker.getValue());

            }
        })
        .setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,
                    int id) {
                dialog.cancel();
            }
        });
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();

I want to align them on equal width.

snapshot


回答1:


You are setting RelativeLayout.LayoutParams on views which are added to LinearLayout. You should use RelativeLayout in place of LinearLayout as parent of NumberPicker. Setting the RelativeLayout.LayoutParams won't work on LinearLayout.

Edit:

I would have used a LinearLayout as parent with setting orientation to Horizontal and children weights to '1'. Which would be something like this...

LinearLayout LL = new LinearLayout(mContext);
LL.setOrientation(LinearLayout.HORIZONTAL);
//
final NumberPicker aNumberPicker = new NumberPicker(mContext);
aNumberPicker.setMaxValue(50);
aNumberPicker.setMinValue(1);
//
final NumberPicker aNumberPickerA = new NumberPicker(mContext);
aNumberPickerA.setMaxValue(11);
aNumberPickerA.setMinValue(1);
aNumberPickerA.setDisplayedValues(new String[] { "Tea Cup", "Glass","Plate","Small Plate","Cutlets","Medium","Piece","Katori","Balls","Serving","egg"});
//
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(50, 50);
params.gravity = Gravity.CENTER;
//
LinearLayout.LayoutParams numPicerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
numPicerParams.weight = 1;
//
LinearLayout.LayoutParams qPicerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
qPicerParams.weight = 1;
//
LL.setLayoutParams(params);
LL.addView(aNumberPicker,numPicerParams);
LL.addView(aNumberPickerA,qPicerParams);

I hope this helps.

Good Luck. :)



来源:https://stackoverflow.com/questions/27525915/i-want-to-show-two-number-picker-using-dialog

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