Remember: Fragment must be placed in the framlayout, otherwise it will not be replaced completely (that is, the original fagment may exist after switching)
Main.xml
<linearlayout 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"
Android:background= "#ffffff"
android:orientation= "Vertical" >
<framelayout
Android:id= "@+id/content"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
>
<fragment
Android:name= "Com.liu25.asmallpark.Fragment.Fragment_1"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
tools:layout= "@layout/fragment_1"/>
</FrameLayout>
<linearlayout
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content" >
<!--four x button>
<LinearLayout/>
<LinearLayout/>
Fragment_1.xml (grey background)
<?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:background= "#f3f3f3"
android:orientation= "Vertical" >
</LinearLayout>
Fragment_2.xml (black background)
<?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:background= "#000000"
android:orientation= "Vertical" >
</LinearLayout>
Main.java
In the button click event:
Fragmentmanager Fragmentmanager = Getfragmentmanager ();//
Fragmenttransaction transaction = Fragmentmanager.begintransaction ();
Transaction.replace (R.id.content, fragment_1);
Transaction.commit ();
Using fragment to add the bottom navigation bar