2013-08-18 43 views
35

我有以下代码工作生成片段,但仅当我将它们添加到存在于我的XML文件中的线性布局时。如何将片段添加到以编程方式生成的布局?

LinearLayout fragmentsLayout = (LinearLayout) findViewById(R.id.foodItemActvity_linearLayout_fragments); 
FragmentManager fragMan = getFragmentManager(); 
FragmentTransaction fragTransaction = fragMan.beginTransaction(); 

Fragment myFrag= new ImageFragment(); 
fragTransaction.add(R.id.foodItemActvity_linearLayout_fragments, myFrag , "fragment" + fragCount); 
fragTransaction.commit(); 

现在,如果我想那个片段添加到尚不在XML文件中存在的线性布局,例如

LinearLayout rowLayout = new LinearLayout(); 

第2部分:

Fragment frag1 = generateAppropriateFragment(type1); 
    Fragment frag2 = generateAppropriateFragment(type2); 

    LinearLayout fragmentsLayout = (LinearLayout) findViewById(R.id.foodItemActvity_linearLayout_fragments); 
    LinearLayout rowLayout = new LinearLayout(this); 
    rowLayout.setId(12345); // add counter to end 

    fragmentsLayout.addView(rowLayout);  
    getFragmentManager().beginTransaction().add(rowLayout.getId(), frag1, "fragment_grandchild" + fragCount).commit(); 
    fragCount++; 
    getFragmentManager().beginTransaction().add(rowLayout.getId(), frag2, "fragment_grandchild" + fragCount).commit(); 
    fragCount++; 

回答

79

在一些点,我想你会将你编程创建的LinearLayout添加到你在.xml中定义的某个根布局。 这只是我的建议,可能是众多解决方案之一,但它的工作原理如下: 只需为编程创建的布局设置ID,并将其添加到您在.xml中定义的根布局,然后将其添加到设置ID以添加片段。

它看起来是这样的:

LinearLayout rowLayout = new LinearLayout(); 
rowLayout.setId(whateveryouwantasid); 
// add rowLayout to the root layout somewhere here 

FragmentManager fragMan = getFragmentManager(); 
FragmentTransaction fragTransaction = fragMan.beginTransaction(); 

Fragment myFrag = new ImageFragment(); 
fragTransaction.add(rowLayout.getId(), myFrag , "fragment" + fragCount); 
fragTransaction.commit(); 

只需选择任何整数值你想的ID:

rowLayout.setId(12345); 

如果使用上面的代码行不只是一次,可能会很聪明地找出创建唯一ID的方法,以便避免重复

UPDATE:

这里是它应该如何完成的完整代码: (此代码测试和工程) 我加入两个片段与水平方向的LinearLayout中,导致碎片彼此相邻排列。请注意,我使用了200dp的固定高度和宽度,因此one Fragment不会像使用“match_parent”一样使用全屏

MainActivity.java:

public class MainActivity extends Activity { 

    @SuppressLint("NewApi") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main);  

     LinearLayout fragContainer = (LinearLayout) findViewById(R.id.llFragmentContainer); 

     LinearLayout ll = new LinearLayout(this); 
     ll.setOrientation(LinearLayout.HORIZONTAL); 

     ll.setId(12345); 

     getFragmentManager().beginTransaction().add(ll.getId(), TestFragment.newInstance("I am frag 1"), "someTag1").commit(); 
     getFragmentManager().beginTransaction().add(ll.getId(), TestFragment.newInstance("I am frag 2"), "someTag2").commit(); 

     fragContainer.addView(ll); 
    } 
} 

TestFragment.java:

public class TestFragment extends Fragment { 

    public static TestFragment newInstance(String text) { 

     TestFragment f = new TestFragment(); 

     Bundle b = new Bundle(); 
     b.putString("text", text); 
     f.setArguments(b); 
     return f; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View v = inflater.inflate(R.layout.fragment, container, false); 

     ((TextView) v.findViewById(R.id.tvFragText)).setText(getArguments().getString("text"));  
     return v; 
    } 
} 

activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/rlMain" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="5dp" 
    tools:context=".MainActivity" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

    <LinearLayout 
     android:id="@+id/llFragmentContainer" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignLeft="@+id/textView1" 
     android:layout_below="@+id/textView1" 
     android:layout_marginTop="19dp" 
     android:orientation="vertical" > 
    </LinearLayout> 
</RelativeLayout> 

片段。XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="200dp" 
    android:layout_height="200dp" > 

    <TextView 
     android:id="@+id/tvFragText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="" /> 

</RelativeLayout> 

这是上面的代码的结果: result

+0

这正是我想要做的(这两个片段被彼此相邻排列)!唯一的事情是我必须设置ID为什么?因为它想要一个INT并且通常在xml文件中id是一个字符串? – Zapnologica

+0

这绝对有效。 @Zapnologica看看所以找到如何生成唯一的ID。但是,使用像'647589'这样的应用程序不应该是个问题。 – Vikram

+0

只要选择你想要的整数作为id :) –

相关问题