<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity"
    android:orientation="horizontal"
    >
 <LinearLayout
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="vertical"
     >
     <Button
         android:onClick="click1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="按钮一"
         />
     <Button
         android:onClick="click2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="按钮二"
         />
     <Button
         android:onClick="click3"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="按钮三"
         />
     <Button
         android:onClick="click4"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="按钮四"
         />

 </LinearLayout>
    <fragment
        android:name="cn.itcase.fragment.Fragment00"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/container"
        />

</LinearLayout>
复制代码
package cn.itcase.fragment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private FragmentManager fm;
    private FragmentTransaction ft;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fm=getSupportFragmentManager();
    }
    public  void click1(View view){
        ft= fm.beginTransaction();
        ft.replace(R.id.container, new Fragment01());
        ft.commit();
}
    public  void click2(View view){
        ft= fm.beginTransaction();
        ft.replace(R.id.container,new Fragment02());
        ft.commit();

    }
    public  void click3(View view){
        ft= fm.beginTransaction();
        ft.replace(R.id.container,new Fragment03());
        ft.commit();

    }
    public  void click4(View view){
        ft= fm.beginTransaction();
        ft.replace(R.id.container,new Fragment04());
        ft.commit();
    }
}
复制代码

 

 
复制代码
复制代码
<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="#ffff00"
    >
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="界面一"
    />

</RelativeLayout>
复制代码
package cn.itcase.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

public class Fragment01 extends Fragment {
    private View view;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity1, container, false);
        return view;
    }

}