<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <FrameLayout android:id="@+id/fl_content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/bt_three" > <fragment android:id="@+id/fragment_one" android:name="tanare.cn.fragment.FragmentOne" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout> <Button android:id="@+id/bt_two" android:layout_width="match_parent" android:layout_height="40dp" android:text="two" android:layout_alignParentBottom="true" android:onClick="doclick" /> </RelativeLayout>
fragmentOne的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="本人是" android:textSize="20sp" /> </RelativeLayout>
fragmentTwo的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="本人是fragmentTwo" android:textSize="20sp" /> </RelativeLayout>
在MainActivity中添加以下代码
public class MainActivity extends FragmentActivity {
private FragmentTwo fTwo;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void doclick(View view) {
// TODO Auto-generated method stub
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
switch (view.getId()) {
case R.id.bt_two:
if(fTwo==null){
fTwo = new FragmentTwo();
}
transaction.replace(R.id.fragment_one, fTwo);
transaction.commit();
break;
}
}
}
运行后出现重叠现象
10
10
15
<FrameLayout
android:id=”@+id/fl_content”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_above=”@+id/bt_three”
>
<fragment
android:id=”@+id/fragment_one”
android:name=”tanare.cn.fragment.FragmentOne”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
/>
</FrameLayout>
原因是你这样布局相当于把fragmentone,add到fl_content这样的话,这个framelayout就有两层了,replace就不能替换fragmentone了,你可以将fragmentone也用replace方式添加进去。或直接用,add和hide组合吧。
5
把R.id.fragment_one改成fl_content。不过一般都是用add,添加进第二个再hide第一个。
改成fl_content了还是替换不了,还是重叠在一起
<FrameLayout
android:id=”@+id/fl_content”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_above=”@+id/bt_three”
>
<fragment
android:id=”@+id/fragment_one”
android:name=”tanare.cn.fragment.FragmentOne”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
/>
</FrameLayout>
原因是你这样布局相当于把fragmentone,add到fl_content这样的话,这个framelayout就有两层了,replace就不能替换fragmentone了,你可以将fragmentone也用replace方式添加进去。或直接用,add和hide组合吧。假如将fragment_one用replace来add进去,那不一样是add了吗?不是同样是两层了?有点追根究底了。
replace是替换不是添加。