我想显示使用对话框两成多选择器 [英] I want to show two number picker using dialog
问题描述
我想只显示用java在对话框两成多选择器,在code是工作,但我不能够安排它的宽度相等。
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.
这是我的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();
我要对齐它们的宽度相等。
快照
snapshot
推荐答案
正在制定的 RelativeLayout.LayoutParams 的意见被加入到的LinearLayout 。你应该使用 RelativeLayout的在的LinearLayout 作为对 NumberPicker 父。设置在 RelativeLayout.LayoutParams 将无法工作在的LinearLayout
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.
编辑:
我会用一个的LinearLayout作为亲与设置取向为水平和儿童的权重为1。这将是这样的...
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.
好运气。 :)
这篇关于我想显示使用对话框两成多选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!