替换Android中的片段

替换Android中的片段,android,fragment,Android,Fragment,我不能用下面的代码替换碎片。我的意思是第二个片段出现在第一个片段下面。第一个碎片没有像我预料的那样隐藏起来。这里怎么了? 非常感谢 我的活动: public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.

我不能用下面的代码替换碎片。我的意思是第二个片段出现在第一个片段下面。第一个碎片没有像我预料的那样隐藏起来。这里怎么了? 非常感谢

我的活动:

public class MainActivity extends Activity  {

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

    final FragmentManager fm = this.getFragmentManager();
    final Fragment fragmenttwo = new FragmentTwo();

    Button fmchangebutton = (Button) this.findViewById(R.id.fmchangebutton);
    fmchangebutton.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {

             FragmentTransaction ft = fm.beginTransaction();
             ft.replace(R.id.fragment1, fragmenttwo);
             ft.commit();

        }

    });

    };

};
activity_main.xml

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

<Button
    android:id="@+id/fmchangebutton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Change Fragment" />

<fragment
    android:id="@+id/fragment1"
    android:name="com.example.fragementex.FragmentOne"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
 </LinearLayout>

fragmnetone.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="clip_horizontal"
    android:ems="10"
    android:gravity="center_vertical|center_horizontal|top|fill_vertical"
    android:text="This is Fragment One" >


</EditText>

</LinearLayout>

fragmnettwo.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="clip_horizontal"
    android:ems="10"
    android:gravity="center_vertical|center_horizontal|top|fill_vertical"
    android:text="This is Fragment Two" >

  </EditText>

 </LinearLayout>

您无法替换附加到布局的片段,您需要创建一个主机容器作为
FrameLayout
,如下所示:

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

      <Button
          android:id="@+id/fmchangebutton"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="Change Fragment" />

      <FrameLayout
          android:id="@+id/frame_layout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />

</LinearLayout>

@Rudi根据参考资料:“FrameLayout设计用于在屏幕上屏蔽一个区域以显示单个项目。通常,FrameLayout应用于保存单个子视图”-您可以使用任何视图组来承载片段(我对其进行了测试!),但您会有奇怪的行为。例如,在LinearLayout中,可以不显示片段,即使stacktrace说它已添加。FrameLayout就是为它而设计的。@Rudi同样,当你动态添加一个片段时,你的活动仍然在后面。因此,主要需要一个视图组,它可以使视图相互重叠。我认为,这就是为什么框架布局是最好的选择。
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // set the layout which contains the framelayout
    setContentView(R.layout.activity_main);

    final FragmentManager fm = this.getFragmentManager();
    final Fragment fragmentone = new FragmentOne();
    final Fragment fragmenttwo = new FragmentTwo();

    // if first initialization
    if(savedInstanceState == null) {
        // display with "add" the first fragment
        this.getFragmentManager().beginTransaction()
            .add(R.id.frame_layout, fragmentone).commit();
    }

    Button fmchangebutton = (Button) this.findViewById(R.id.fmchangebutton);
    fmchangebutton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            FragmentTransaction ft = fm.beginTransaction();
            // "replace" the fragment inside the framelayout
            ft.replace(R.id.frame_layout, fragmenttwo);
            ft.commit();
        }
    });

}