以下是MainActivity的布局文件
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
android:id="@+id/fl_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bt_three"
>
android:id="@+id/fragment_one"
android:name="tanare.cn.fragment.FragmentOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
android:id="@+id/bt_two"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="two"
android:layout_alignParentBottom="true"
android:onClick="doclick"
/>
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="本人是"
android:textSize="20sp"
/>
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="本人是fragmentTwo"
android:textSize="20sp"
/>
在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;
}
}
}
运行后出现重叠现象