效果图如下:

项目结构图如下:

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

@Override

public 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

@Override

public 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 {

@Override

protected 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){

//说明是竖屏 加载一个fragment

beginTransaction.replace(android.R.id.content, new Fragment1());

}else{

//说明是横屏

beginTransaction.replace(android.R.id.content, new Fragment2());

}

//最后一步 记得commit

beginTransaction.commit();

}

}

fragemnt1.xml:

xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:text="我是fragment1竖屏的内容"

android:textSize="20sp"

/>

fragemnt2.xml:

xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:text="我是fragment2横屏的内容"

android:textSize="30sp"

/>

android fragment动态添加,Android动态添加Fragment相关推荐

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

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

  2. android listview动态添加viewpager,向 ViewPager 中添加 包含 ListView 的 Fragment

    对与fragment就不说什么了,直接看API手册吧,亲. 向 ViewPager 中添加 包含 ListView 的 Fragment 的过程比较麻烦.他所表现的效果就是新闻客户端的滑动翻页效果. ...

  3. Android组件化专题-路由动态注入跳转参数以及获取其他模块的fragment

    上几篇组件化专题文章,我们对路由框架基本实现了,Activity之间当跳转和模块间当通信. 现在思考一个问题: 不管是在Activity或者Fragment,跳转都会传递一些参数,然后在对应对Acti ...

  4. Android编程权威指南笔记3:Android Fragment讲解与Android Studio中的依赖关系,如何添加依赖关系

    Android Fragment 当我在学习时,了解了Fragment词汇 Fragment是一种控制器对象,我就把所了解的简单说一下.activity可以派fragment完成一些任务,就是管理用户 ...

  5. [Android] 通过GridView仿微信动态添加本地图片

    原文:http://blog.csdn.net/eastmount/article/details/41808179 前面文章讲述的都是"随手拍"中图像处理的操作,此篇文章主要讲述 ...

  6. Android fragmnet标签,在Android中为Fragment添加标签?

    我试图在Fragment中添加一个TabHost.代码如下.这里里面的Fragment.我试图添加TabHost显示两个标签: package com.nordicsoft.dilosysNewVer ...

  7. android 点动态显示图片,Android用RecyclerView实现动态添加本地图片

    本文介绍了Android用RecyclerView实现动态添加本地图片,分享给大家,具体如下: 简单介绍一下用法: 1.跳转到图片选择页面: Intent intent = new Intent(Pa ...

  8. Android学习笔记(十一)——将Fragment添加到Activity中以及参数传递

    将Fragment添加到Activity中 Fragment的布局文件 <LinearLayout xmlns:android="http://schemas.android.com/ ...

  9. android 动态单选,添加单选按钮动态︰android

    我需要添加单选按钮动态,意味着单选按钮可能是3,4,5或6,它将被添加水平和一行包含最多3单选按钮,如果有更多然后3那么它会出现在网格视图上方的单选按钮的下方,我在这里工作2天,但没有得到任何解决方案 ...

最新文章

  1. Python库全部整理出来了,非常全面
  2. System.gc()与Object.finalize()的区别
  3. 【java】java 使用 jmap 工具 DebuggerException
  4. 基于JAVA+SpringMVC+Mybatis+MYSQL的高校勤工助学管理系统
  5. 【贪心】 【HDU 5821】 Ball
  6. VXLAN配置实例(一)——VXLAN基本配置
  7. 基于Web的svg编辑器(2)——层次结构设计(DOM结构)
  8. nb物联网 bc95模块联网 bc95使用
  9. 聊聊我的2021,总结与展望
  10. Sass基础学习(一)
  11. 计算机网络课制作双绞线实验,计算机网络实验一(双绞线的制作)
  12. win 10 PHP开发环境配置
  13. CSS网页布局中易犯的30个小错误
  14. 关于U盘存储大量小容量文件速度慢的解释(摘抄)
  15. Instruction set
  16. matlab验证叠加定理,叠加原理和戴维南定理实验报告
  17. 转载-【讨论】为什么不去读顶级会议上的论文?适应于机器学习、计算机视觉和人工智能
  18. python的subprocess模块
  19. It is indirectly referenced from required .class files
  20. 区块链为供应链建立全链路溯源防伪体系提供保障

热门文章

  1. shell 指令使用整理
  2. 蚂蚁金服开源自动化测试框架 SOFAACTS
  3. 操作系统/etc/hosts文件配置
  4. sql server 计算两个时间 相差的 几天几时几分几秒
  5. postgres循环sql
  6. 15.4. syslog, klogctl - read and/or clear kernel message ring buffer; set console_loglevel
  7. AESNI/XData勒索病毒来袭 目前主要在乌克兰传播 它居然还能使用硬件加速加密过程...
  8. Redis内存分析工具
  9. 在linux的weblogic上增加启动参数
  10. 算法7-10:拓扑排序