2015-01-13 69 views
0

所以我很新的Android開發,我有以下情況:安卓:模板與XML

<LinearLayout 
    android:id="@id/child_1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <TextView 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text=""> 

    <Button 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text=""> 
</LinearLayout> 

的問題是,我不得不重複這個水平的LinearLayout次未知量,爲每個新實例動態添加TextView和Button的文本字段。在水平的LinearLayout的每個新實例必須被添加到現有的垂直的LinearLayout。

因此,最初的垂直的LinearLayout:

<LinearLayout 
    android:id="@id/parent" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

</LinearLayout> 

然後根據動態實現:

<LinearLayout 
    android:id="@id/parent" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:id="@id/child_1">   
    </LinearLayout> 

    <LinearLayout 
     android:id="@id/child_2">   
    </LinearLayout> 

</LinearLayout> 

是沒有什麼辦法,跟我描述可以做到這一點?我在示例中使用的XML元素是基本情況。我實際上使用的是與它們相關的樣式屬性,所以我只想在XML中創建第一個LinearLayout,然後以編程方式訪問它,添加必需的字段,然後將其設置爲垂直的的垂直 LinearLayout 。

+0

使用[列表視圖](http://developer.android.com/guide/topics/ui/layout/listview .html)。您可以根據需要自定義每行的外觀。 – Hemanth

回答

0

您可以動態創建孩子的,並將它們添加到父垂直的LinearLayout。 請檢查下面的代碼:我用於每個創建的行一個TextView和一個微調:

//--- Row Layout --- 
    LinearLayout rowLayout = null; 
    LinearLayout.LayoutParams rowLayoutparams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 

    /** Create the TextViews and Spinners */ 
    for (int j=0; j<gameArray.length; j++) { 
     //Create new row 
     rowLayout = new LinearLayout(getActivity().getApplicationContext()); 
     rowLayout.setLayoutParams(rowLayoutparams); 
     rowLayout.setGravity(Gravity.CENTER_HORIZONTAL); 

     //--- TEXT VIEW --- 
     TextView myTextView = new TextView(getActivity().getApplicationContext()); 
     myTextView.setId(j); 
     myTextView.setText("CONTENT"); 
     myTextView.setGravity(Gravity.CENTER); 

     //--- SPINNER --- 
     spinnersArray[j] = new Spinner(getActivity().getApplicationContext()); 
     spinnersArray[j].setId(j+100); //set a different ID 
     //Populate the spinner here ... 

     //--- LAYOUT PARAMS --- 
     LinearLayout.LayoutParams itemParams = new LinearLayout.LayoutParams(100, 100);  //first and last item 
     //edit the item params here ... 

     //Add the params to the Views 
     myTextView.setLayoutParams(itemParams); 
     spinnersArray[j].setLayoutParams(itemParams); 

     //--- Add the view to the Layout --- 
     rowLayout.addView(myTextView); 
     rowLayout.addView(spinnersArray[j]); 

     //--- Add the row to the layout only if (NOT even) OR the chars are finished ---   
     ((LinearLayout)getView().findViewById(R.id.parent)).addView(rowLayout); 
    }