2012-03-03 110 views
3

我想为使用片段3.0构建一个应用程序。在应用程序的左侧有一个静态片段,右侧有一个动态片段。动态部分中的每个片段都有一个标题。每当我去替换最初的片段时,第一个的标题仍然显示在第一个的标题上方。连续替换替换较低部分,但初始标题仍显示(抱歉,我无法发布图像)。Android片段没有正确替换

最佳我可以用于图像做:

初始部首

新头部

内容

main.xml中(显示精细):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 
    <fragment 
     android:id="@+id/static_fragment" 
     android:name="ruleout.android.History_Fragment" 
     android:layout_width="500dp" 
     android:layout_height="match_parent" /> 
    <fragment 
     android:id="@+id/dynamic_fragment" 
     android:name="ruleout.android.New_Screen_Fragment" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" /> 
</LinearLayout> 

偏MainActivity使用改变片段

private void open(Fragment toShow) 
{ 
    FragmentTransaction fragmentTransaction = FragMag.beginTransaction(); 
    fragmentTransaction.replace(R.id.dynamic_fragment, toShow); 
    fragmentTransaction.addToBackStack(null); 
    fragmentTransaction.commit(); 
} 

请让我知道,如果您需要任何额外的信息,并提前感谢您的帮助。

回答

8

如果要动态更改片段,则不能在布局中声明它们。 Fragment documentation举例说明了我正在描述的内容,其中布局中已声明了一个静态片段以及用于保存将动态添加和替换的Fragment的FrameLayout。

+0

感谢您的帮助。它效果很好。 – cren90 2012-03-03 07:03:11