动态地向我的活动布局添加多个片段

use*_*311 8 android

我有一个没有子视图的linearlayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >


</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我想根据需要动态添加一个片段或两个片段.我知道添加一个片段,但我怎么能动态地向它添加两个片段.我有两个片段,在每个片段中我写下以下oncreateview

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        //return super.onCreateView(inflater, container, savedInstanceState);
        View v=inflater.inflate(R.layout.frag1, container, false);
        v.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

        return v; 
    }
Run Code Online (Sandbox Code Playgroud)

我试图使用以下代码添加,但第二个片段来自第一个.

FragmentTransaction ft=fm.beginTransaction();
        frag1 f=new frag1();
        frag2 ff=new frag2();


        ft.add(android.R.id.content, f);
        ft.add(android.R.id.content, ff);

        ft.commit();
Run Code Online (Sandbox Code Playgroud)

请更新如何做到这一点.谢谢

Dan*_* Bo 0

这应该可以解决这个问题:

    FragmentTransaction ft=fm.beginTransaction();
    frag1 f=new frag1();
    frag2 ff=new frag2();
    LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

    ft.add(android.R.id.content, f).commit();
    FragmentTransaction ft=fm.beginTransaction(); // this line might be unnescessary
    ft.add(android.R.id.content, ff).commit();
Run Code Online (Sandbox Code Playgroud)