【发布时间】:2015-06-02 23:07:14
【问题描述】:
我正在创建一个警报对话框,其中包含两个数字选择器和两个文本视图,用于选择月份和年份,文本视图只是标签。 我正在动态创建它们,因为填充 min 和 max 选项似乎更容易(无法使用 xml 和布局充气机来完成)。我只是不能让两个选择器(及其标签)集中在警报对话框上。它们显示在极左和极右,使其非常难看。 我可能编码过度了,但这里是:
final NumberPicker np1= new NumberPicker(this);
final NumberPicker np2= new NumberPicker(this);
final TextView t1=new TextView(this);
final TextView t2=new TextView(this);
t1.setText("Month");
t1.setTextSize(20);
t2.setText("Year");
t2.setTextSize(20);
np1.setMaxValue(12);
np1.setMinValue(1);
np2.setMaxValue(2030);
np2.setMinValue(2000);
np1.setValue(Month);
np2.setValue(Year);
RelativeLayout linearLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(20,20);
RelativeLayout.LayoutParams numPicerParams1=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams numPicerParams2=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
numPicerParams1.addRule(RelativeLayout.LEFT_OF);
numPicerParams2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
linearLayout.setLayoutParams(params);
linearLayout.addView(np1, numPicerParams1);
linearLayout.addView(np2, numPicerParams2);
linearLayout.addView(t1,numPicerParams1);
linearLayout.addView(t2,numPicerParams2);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Chose month and year.");
builder.setView(linearLayout);
builder.setPositiveButton("OK",new DialogInterface.OnClickListener()
{ (...)
【问题讨论】:
-
你能发布你的xml吗?
-
@JohnWhite - 我认为他正在动态创建所有内容。
标签: android alignment numberpicker