效果图如下:

项目结构图如下:

Fragment1:

package com.demo.dongtaifragment;import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;public class Fragment2 extends Fragment {//显示faragemnt1 自己要显示的内容@Nullable@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragemnt2, null);return view;}
}

Fragment2:

package com.demo.dongtaifragment;import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;public class Fragment2 extends Fragment {//显示faragemnt1 自己要显示的内容@Nullable@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragemnt2, null);return view;}
}

MainActivity:

package com.demo.dongtaifragment;import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//[1]获取手机的分辨率WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);int width = wm.getDefaultDisplay().getWidth();int height = wm.getDefaultDisplay().getHeight();FragmentManager fragmentManager  = getFragmentManager();//开启事务FragmentTransaction beginTransaction = fragmentManager.beginTransaction();Fragment1 fragment1 = new Fragment1();//判断横竖if(height>width){//说明是竖屏  加载一个fragmentbeginTransaction.replace(android.R.id.content, new Fragment1());}else{//说明是横屏beginTransaction.replace(android.R.id.content, new Fragment2());}//最后一步 记得commitbeginTransaction.commit();}
}

fragemnt1.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="match_parent"android:text="我是fragment1竖屏的内容"android:textSize="20sp"/></android.support.constraint.ConstraintLayout>

fragemnt2.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="match_parent"android:text="我是fragment2横屏的内容"android:textSize="30sp"/></android.support.constraint.ConstraintLayout>

转载于:https://www.cnblogs.com/charlypage/p/10353231.html

Android动态添加Fragment相关推荐

  1. android fragment动态添加,Android动态添加Fragment

    效果图如下: 项目结构图如下: Fragment1: package com.demo.dongtaifragment; import android.app.Fragment; import and ...

  2. android fragment动态加载,动态添加Fragment

    8种机械键盘轴体对比 本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选? 动态添加Fragment 在程序运行时, 根据具体情况来动态地添加Fragment到Activity中. 1 新建 An ...

  3. Android动态添加Device Admin权限

    /*********************************************************************** Android动态添加Device Admin权限* ...

  4. Android 动态添加标签及其点击事件

    在做Android开发的时候,会遇到动态添加标签让用户选择的功能,所以自己写了个例子,运行效果图如下. 标签可以左右滑动进行选择,点击的时候,会弹出toast提示选择或者取消选择了哪个标签.通过动态添 ...

  5. Android 动态添加Button(1)

    这是一个非常简单的动态添加Button的例子: package com.kkkk.llll;import android.app.Activity; import android.os.Bundle; ...

  6. android 动态生成fragment,Android动态加载fragment(fragment复用)

    [实例简介] Android动态加载fragment(fragment复用) [实例截图] [核心代码] fm_reuse └── fm_reuse ├── AndroidManifest.xml ├ ...

  7. android动态居中布局,Android动态添加布局的两种方式

    释放双眼,带上耳机,听听看~! 前言 大多数时候我们布局都是用xml来布局的,但有些时候也是会用到动态布局的,尤其是在一些大项目中,动态布局更是体现的淋漓尽致. 所以今天我们就来学习一些动态加添布局的 ...

  8. android动态改变布局,Android 动态添加布局的两种方式

    前言 大多数时候我们布局都是用xml来布局的,但有些时候也是会用到动态布局的,尤其是在一些大项目中,动态布局更是体现的淋漓尽致. 所以今天我们就来学习一些动态加添布局的两种方式,分别是 动态添加xml ...

  9. Android动态添加5个图片资,Android绘图实例(Bitmmap,Canvas,Pain的使用)动态的在图片上添加文字(canvas.drawText)...

    近些天学习图像处理,今天看见了一段代码中有Bitmap时,找了些Bitmap的资料,有查看了与之有关的绘图类. BitMap代表一张位图,BitmapDrawable里封装的图片就是一个Bitmap对 ...

最新文章

  1. 去除菜单项的加速键--‘’符号
  2. 编译安装rabbitmq服务端
  3. 20165201 实验五 网络编程与安全
  4. 脉冲神经网络_【强基固本】脉冲神经网络(SNN)
  5. 图文并茂,详细讲解UML类图符号、各种关系说明以及举例
  6. wifi 2.4g 5g 区别_wifi信号差,网速慢?可能是你没有配置好2.4G和5G WiFi
  7. ACDream - Dynamic Inversions II
  8. 【STL源码学习】std::list类的类型别名分析
  9. GDB中创建要素数据集
  10. 恒温控制系统单片机仿真c语言,基于51单片机的恒温控制系统
  11. GRE tunnel 2
  12. 201903-2 二十四点
  13. 用手机当电脑摄像头的方法
  14. 联想System X 3650M5 服务器装机问题记录
  15. 计算机网络之网络层-网络层拥塞控制
  16. Java web接入google身份验证器二次验证
  17. bzoj3168-钙铁锌硒维生素
  18. windows10小技巧: 将手机投影到windows10上
  19. iOS开发storyboard拖拽tableView: Static cells的使用
  20. 139.深度学习分布式计算框架-2

热门文章

  1. java基础的知识_Java基础知识点(一)
  2. linux运维中的中间件,linux中间件Nginx的安装过程
  3. python计算文件大小的方法_使用Python计算目录的大小?
  4. SpringBootSwagger构建REST API并生成API文档
  5. Ubuntu 18.04安装软件包下载速度慢的解决方案
  6. Kotlin入门(6)条件分支的实现
  7. [绝对原创]从VS2003(.net1.1)升级到vs2005(.net2.0)全程跟踪记录
  8. 显著性检测(saliency detection)评价指标之KL散度距离Matlab代码实现
  9. 《jQuery UI 开发指南》——2.3 tabs ()方法
  10. tomcat之一:指定tomcat运行时JDK版本