2015-06-02 69 views
0

我創建一個警告對話框有兩個號碼採摘和每個人的前兩名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() 
    { (...) 
+0

您可以發佈您的XML? –

+0

@JohnWhite - 我認爲他是動態創建的一切。 –

回答

0

試試這個:

RelativeLayout relativeLayout = new RelativeLayout(this); 
relativeLayout.addRule(RelativeLayout.CENTER_IN_PARENT); 


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); 

relativeLayout.addView(linearLayout); 
+0

這層他們在彼此的頂部,不是嗎? –

+0

看起來像this.Eclipse說,該方法是未定義類型的RelativeLayout我不能RelativeLayout的使用.addrule。 – FFP

0

你的XML應該是這個樣子:

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true"> 
    <TextView 
     android:id="@+id/month_text" 
     android:text="Month" 
     android:layout_width="wrap_content" 
     android:layout_alignLeft="@id/month_picker" 
     android:layout_alignRight="@id/month_picker" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal"/> 


    <TextView 
     android:id="@+id/day_text" 
     android:text="Year" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/month_text" 
     android:layout_alignLeft="@id/year_picker" 
     android:layout_alignRight="@id/year_picker" 
     android:gravity="center_horizontal"/> 

    <NumberPicker 
     android:id="@+id/month_picker" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/month_text" /> 

    <NumberPicker 
     android:id="@+id/year_picker" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/day_text" 
     android:layout_toRightOf="@id/month_picker"/> 

</RelativeLayout> 

這將大大父裏面佈局是您的警報對話框的主體。

然後,在代碼中,你可以這樣做:

NumberPicker monthPicker = (NumberPicker) view.findViewById(R.id.month_picker); 
NumberPicker yearPicker = (NumberPicker) view.findViewById(R.id.year_picker); 

monthPicker.setMaxValue(12); 
monthPicker.setMinValue(1); 
yearPicker.setMaxValue(2030); 
yearPicker.setMinValue(2000); 
+0

我很抱歉,但我怎麼膨脹(呼叫)的XML?什麼進入警報對話框的正文? – FFP

+0

[AlertDialog.Builder](http://developer.android.com/reference/android/app/AlertDialog.Builder.html)爲您提供了一條建立AlertDialog的非常清晰的路徑,您可以設置AlertDialog通過調用'AlertDialog.Builder.setView(view)'。傳遞給此方法的視圖將是上面的xml,它使用'LayoutInflater'膨脹。 –