public class MyFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.my_fragment, container, false); TextView textView = view.findViewById(R.id.text_view); textView.setText("Hello Fragment!"); return view; } } public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); MyFragment fragment = new MyFragment(); fragmentTransaction.add(R.id.fragment_container, fragment); OtherFragment otherFragment = new OtherFragment(); fragmentTransaction.replace(R.id.fragment_container, otherFragment); fragmentTransaction.commit(); } }


上一篇:
下一篇:
切换中文