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

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

  <LinearLayout/>

<LinearLayout/>

fragment_1.xml(灰色背景)

<?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(黑色背景)

<?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
在Button点击事件里面:
  

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

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

 

最新文章

  1. VirtualBox动态添加虚拟硬盘
  2. JSR 303 - Bean Validation 介绍及最佳实践
  3. matlab cross 3*1 向量叉乘
  4. lucas 定理学习
  5. linux权威指南 简记
  6. javascript基础学习(四)
  7. 【移动开发】Android中WIFI开发总结(一)
  8. OCP-1Z0-051-题目解析-第11题
  9. Chapter 2 Open Book——20
  10. 【Python】Non-ASCII character '\xe6' 错误解决方法
  11. Android破解学习之路(二)——Android游戏 滚动的天空破解
  12. java程序的加载过程
  13. C++笔记019:C++中的const修饰的是一个真正的常量
  14. 使用VC建立网络连接并访问网络资源
  15. .NET应用程序管理服务AMS设计
  16. iperf 2.05版本升级到2.0.9
  17. 一些常用的mysql语句实例-以后照写2
  18. php根据命令行参数生成配置文件
  19. Qt编写视频播放器(vlc内核)
  20. 如果不用jQuery,Ajax你还能写出多少?

热门文章

  1. tun笔记
  2. Excel-字符串连接
  3. kubernetes实战(十六):k8s高可用集群平滑升级 v1.11.x 到v1.12.x
  4. qt——QObject 与 QWidget 的区别
  5. 【F12】九个Console命令,让js调试更简单
  6. SQL Server表分区-水平分区
  7. vue使用resource传参数
  8. Bus System(Flody)
  9. PAT 1032 Sharing[hash][链表][一般上]
  10. python3中替换python2中cmp函数