1、视图

1)主视图

 <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:orientation="horizontal"
tools:context=".MainActivity" > </LinearLayout>

2)fragment1视图

 <?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="#ff0000"
android:orientation="vertical" > </LinearLayout>

3)fragment2视图

 <?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="#0000ff"
android:orientation="vertical" > </LinearLayout>

2、java代码

1)切换代码

 package com.example.myfragment;

 import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //判断手机当前朝向
int width = getWindowManager().getDefaultDisplay().getWidth();
int height = getWindowManager().getDefaultDisplay().getHeight(); Fragment1 fragment1 = new Fragment1();
Fragment2 fragment2 = new Fragment2();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
//R.id.content说明是当前的模板
if(width > height){
//水平
ft.replace(android.R.id.content, fragment1);
}else{
//垂直
ft.replace(android.R.id.content, fragment2);
} ft.commit();
} }

2)fragment1和fragment2代码

 package com.example.myfragment;

 import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class Fragment2 extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment2, null);
} }

fragment的切换的更多相关文章

  1. ViewPager之Fragment页面切换

    一.概述 ViewPager是android-support-v4中提供的类,它是一个容器类,常用于页面之间的切换. 继上篇文章<ViewPager之引导页>之后,本文主要介绍ViewPa ...

  2. 仿知乎程序 fragment的切换以及toolbar在不同页面下显示的menu不同

           我们在看知乎的时候,你会发现,首页,发现,关注,收藏,草稿这五项,你在点击之后进入到相应页面之后,侧滑菜单还在,你左侧滑一下,这个侧滑菜单还在,而提问,左滑屏幕,这个页面就没有,有点像返 ...

  3. Fragment保持状态切换,fragment状态切换

    在使用Activity管理多个Fragment时,每次切换Fragment使用的是replace,结果导致出现xxx is not currently in the FragmentManager异常 ...

  4. Fragment与Fragment相互切换之间的生命周期方法

    Fragment 1 切换到 Fragment 2时生命周期变化 1.通过 add hide show 方式来切换 Fragment Fragment1 的生命周期变化为:onCreate().onC ...

  5. AndroidStudio制作底部导航栏以及用Fragment实现切换功能

    前言 大家好,给大家带来AndroidStudio制作底部导航栏以及用Fragment实现切换功能的概述,希望你们喜欢 学习目标 AndroidStudio制作底部导航栏以及用Fragment实现切换 ...

  6. fragment的切换(解决REPLACE的低效)

    在项目中切换Fragment,一直都是用replace()方法来替换Fragment.但是这样做有一个问题,每次切换的时候Fragment都会重新实列化,重新加载一次数据,这样做会非常消耗性能用用户的 ...

  7. 关于含RecyclerView的fragment来回切换时页面自动滑动到底部的解决方法

    原因: 在fragment中来回切换时RecyclerView获得了焦点,而RecyclerView的 focusableOnTouchMode属性默认是true,所以在切换时RecyclerView ...

  8. [Android] 使用Include布局+Fragment滑动切换屏幕

        前面的文章已经讲述了"随手拍"项目图像处理的技术部分,该篇文章主要是主界面的布局及屏幕滑动切换,并结合鸿洋大神的视频和郭神的第一行代码(强推两人Android博客),完毕了 ...

  9. Android基础——Fragment控制切换多个页面

    今天接着上一篇文章,讲解一下Fragment的控制,主要是切换View和页面替换等操作.还有就是如何获取Fragment的管理对象,以及与Activity的通信方式. (PS:新建的QQ群,有兴趣可以 ...

随机推荐

  1. python下线程以及锁

    1.python多线程 #encoding=utf-8 """ python多线程,并非真正意义上的多线程 全局锁:在指定时间里,有且只有一个线程在运行 "&q ...

  2. C#语言基础之运算符

    运算符分类.优先级 运算符:一.数学运算符:+,-,*,/,++,-- 示例1: 示例2: 示例3: 1.递增运算符:++(1)前缀递增运算符    int x=4;    x++;//输出结果,x的 ...

  3. WRTnode 的 HTTP Web PWM 调光实验(2016-05-16)

    前言 这里是节取自 物联网的任意门——WRTnode2R 评测 中的 http web PWM 调光灯实验,所以有一些前置设置如果没有描述清楚可参考该处. 正文 步骤一:编辑 html 文件放在 /w ...

  4. C++改变编程入口为main函数

    1, 你用vc建了一个控制台程序,它的入口函数应该是main, 而你使用了WinMain. 2.  你用vc打开了一个.c/.cpp 文件,然后直接编译这个文件,这个文件中使用了WinMian而不是m ...

  5. 高级UNIX环境编程

    常规文件 只有索引节号(i-number,是一批i-node的索引),i-node不含文件名及数据字节,包括文件类型,链接数量,uid,gid,访问权限,字节数,最后访问时间,最后修改时间,信息节点最 ...

  6. jQuery tablesort插件推荐

    搜索结果的第一条网址(似乎是Official Site)似乎有问题(也可能是我弄错了  总之chrome中有个叉叉) 所以还是用这个吧http://mottie.github.io/tablesort ...

  7. 转: 模块化开发框架seajs简介

    JavaScript模块化开发库之SeaJSSeaJS由国内的牛人lifesinger开发.目前版本是1.1.1,源码不到1500行,压缩后才4k,质量极高.这篇会讲述SeaJS的一些基本用法,不会面 ...

  8. 让我们共同构筑物联网起飞的平台:物联网操作系统Hello China寻求应用合作伙伴

    经过几天的努力,终于把Hello China V1.76版的内核移植到基于Cortex-M3内核的STM32 chipset上.因为还希望进一步写一个USART驱动程序,因此详细的移植文档,预计一周之 ...

  9. pl/sql 的 put和put_line区别

    在学习PL/SQL脚本时,打印语句是用得最多的语句. 在Oracle中,又有两种打印的方法:put和put_line.它们的区别是:put:不换行输出,输出在缓冲区,不显示出来,直到执行put_lin ...

  10. Handler总结

    一.整体工程图 二.activity_handler.xml <?xml version="1.0" encoding="utf-8"?> < ...