我創建一個警告對話框有兩個號碼採摘和每個人的前兩名textviews選擇月份和年份,以及textviews只是標籤。 我動態創建它們,因爲它似乎更容易,以填補最小和最大選項(不能使用XML和佈局充氣做)。我不能讓這兩個選擇器(及其標籤)集中在警報對話框中。他們表現在極左和極右,使其非常醜陋。 我可能在編碼這一點,但在這裏它是:ANDROID:ALIGN動態創建多個選擇器
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 - 我認爲他是動態創建的一切。 –