切记:fragment一定要放在framlayout中,不然不会被替换完全(就是切换之后原来的fagment可能还会存在)

main.xml

<LinearLayout xmlns:andro

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:>
  <!--四个Button>

  <LinearLayout/>



<LinearLayout/>

fragment_1.xml(灰色背景)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro>
</LinearLayout>

fragment_2.xml(黑色背景)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro>
</LinearLayout>

main.java
在Button点击事件里面:
  

FragmentManager fragmentManager = getFragmentManager();//
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content, fragment_1);
transaction.commit();

 

彩蛋:在fragment中获得Context:view.getContext()

 

相关文章:

  • 2021-12-16
  • 2021-05-15
  • 2022-01-29
  • 2021-11-07
  • 2021-11-24
  • 2022-01-07
  • 2022-01-07
猜你喜欢
  • 2021-11-28
  • 2022-01-08
  • 2021-09-07
  • 2021-06-01
  • 2022-12-23
  • 2021-07-20
  • 2022-12-23
相关资源
相似解决方案