簡體   English   中英

如何將更多同一類的片段添加到布局中?

[英]How can I add more fragments of the same class to a Layout?

我有以下代碼:

FragmentA myFragment = new FragmentA(); 

transaction.add(R.id.my_fragment_container, myFragment);

myFragment = new FragmentA(); 

transaction.add(R.id.my_fragment_container, myFragment);

transaction.commit();

所以我想要發生的是,這將創建2個片段。 兩者之一應放在另一個之下。

我的布局如下所示:

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

<LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:id="@+id/my_fragment_container"/>


</LinearLayout>

那么我該怎么做,添加了2個片段,其中一個片段在另一個片段之下?

問候西蒙

1)將以下代碼設為單獨的XML布局文件:

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

2)使用代碼對其進行充氣,並獲取充氣布局的ID。

3)以編程方式將膨脹后的布局添加到id=my_fragment_container的布局中。

4)使用誇大的布局ID將片段添加到FragmentManager。

FragmentA myFragment = new FragmentA(); 
transaction.add(layout_id, myFragment);

我相信這應該起作用,歡呼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM