0

次のようなレイアウト ファイルを使用したアクティビティがあります。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:fillViewport="true"
    android:gravity="top" >

    <LinearLayout
        android:id="@+id/foodItemActvity_linearLayout_fragments"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

   //>>>> Fragments to go here

    </LinearLayout>
</ScrollView>

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.35"
    android:gravity="bottom"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:onClick="orderFoodItemClicked"
        android:text="Order this Item Now" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:onClick="backToMenuItemClicked"
        android:text="Back To Menu" />
</LinearLayout>

次に、それぞれ独自のレイアウト ファイルとクラスを使用して 5 つのフラグメントを既にコーディングしていますが、これは今のところ関係ありません。この例で呼び出されたとします。

fragment1.java fragment2.java fragment3.java

上記のように、線形レイアウトにフラグメントを動的に追加する方法を知りたいです。私の調査から、フラグメントマネージャーと LayoutInflater が必要なようですが、これを行う方法がわかりません。

これまでのところ、私はこれを持っています:

LinearLayout fragmentsLayout = (LinearLayout) findViewById(R.id.foodItemActvity_linearLayout_fragments);

    for(int i=0; i < noOfFragments;i++){
        //Add fragment to fragmentsLayout
    }

今、私が望む特定のフラグメントを追加する方法がわかりませんか?

編集:フラグメントのレイアウトパラメータなどの詳細を指定するにはどうすればよいですか?

4

2 に答える 2

1
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
for(int i=0; i < noOfFragments;i++){
       ft.add(R.id.foodItemActvity_linearLayout_fragments, fragments[i], "fragment tag" + i);
}
ft.commit();
于 2013-08-17T21:31:13.693 に答える