Android:使用XML进行模板化

时间:2015-01-13 17:46:48

标签: android xml android-layout

所以我对Android开发很新,我有以下情况:

<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。

所以最初的Vertical 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。

1 个答案:

答案 0 :(得分:0)

您可以动态创建子项并将它们添加到父级垂直LinearLayout。 请检查以下代码:我为每个创建的行使用了TextView和Spinner:

    //--- 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);
    }
相关问题