Android动态添加Fragment
效果图如下:
项目结构图如下:
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:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android: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.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="我是fragment2横屏的内容"
android:textSize="30sp"
/>
</android.support.constraint.ConstraintLayout>
Android动态添加Fragment的更多相关文章
- Android Fragment用法详解(2)--动态添加Fragment
在上一篇文章<Android Fragment用法详解(1)--静态使用Fragment>我们讲解了Fragment的最简单的用法.这次我们来说一说Fragment复杂一丢丢的用法.在代码 ...
- 使用Fragment 实现动态UI 和 动态添加Fragment
首先写好每个Fragment: 1.在第一个Fragment写一个按钮,使其加载下一个Fragment 布局: <LinearLayout xmlns:android="http:// ...
- Android动态添加Device Admin权限
/********************************************************************** * Android动态添加Device Admin权限 ...
- Android 动态创建Fragment
Fragment是activity的界面中的一部分或一种行为.可以把多个Fragment组合到一个activity中来创建一个多界面并且可以在多个activity中重用一个Fragment.可以把Fr ...
- 动态添加Fragment
在Fragment简单用法的基础上做修改 一.新建:another_right_fragment.xml <LinearLayout xmlns:android="http://sch ...
- Android动态添加碎片
我们编写一个能够用过按钮动态更替碎片的APP,首先在主页上显示第一个碎片,点击按钮后可以替换到第二个碎片,或者删除已经替换掉的第二个碎片. 一.MainActivity.java import and ...
- Android 动态添加Spinner(.java文件内实现) 实现 改变spinner 内文字属性
动态添加spinner 控件 Spinner s = new Spinner(this); String []items={"自己定义的要显示的数组"}; my_SpinnerAd ...
- Android动态添加和移除布局
package com.hyang.administrator.studentproject; import android.os.Bundle; import android.support.v7. ...
- Android — — —动态添加碎片
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" ...
随机推荐
- 100-days: twelve
Title: Mark Zuckerberg wants Facebook's to emulate China's WeChat, but can it? emulate v.效仿,模仿 As ...
- [leetcode]41. First Missing Positive第一个未出现的正数
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- cookie、sesstion、strorage
1. cookie 第一:每个特定的域名下最多生成20个cookie (数量上) <IE6: 20 | >ie7:50 | firefox: 50 | IE ...
- js函数内未声明变量
<script> function test(){ testd = "Hello"; } test(); alert(testd); </script> 当 ...
- ORM初识和数据库操作
ORM简介 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术.简单的说,ORM是通过使用描述对象和数据库之 ...
- 选择困难症的福音——团队Scrum冲刺阶段-Day 1领航
选择困难症的福音--团队Scrum冲刺阶段-Day 1领航 各个成员在 Alpha 阶段认领的任务 小组成员 分工 任务量 严域俊 完成小游戏接口部分.小游戏编写部分 21 吴恒佚 决策判断部分.小游 ...
- rpc和http
rpc,远程过程调用,分布式各服务在不同的节点,因为不在同一进程中,所以节点间的调用需要通过网络进行传输,rpc是基于tcp/ip的,通过长连接进行通信.客户端需要缓存服务端的ip和端口,服务端也要缓 ...
- SpringMVC学习笔记:拦截器和过滤器
首先说明一下二者的区别: 1. 拦截器基于java的反射机制,而过滤器是基于函数回调 2. 拦截器不依赖于servlet容器,过滤器依赖servlet容器 3. 拦截器只能对action请求起作用,而 ...
- 【ZooKeeper】ZooKeeper安装及简单操作
ZooKeeper介绍 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一 ...
- 1.2万事开头hello world+交互+getpass、sys模块初识
1.python的hello world: ①运行cmd-输入python-输入print (“hello world!”) ②创造.py的文本helloworld.py(后缀是为了告诉其他人)-输入 ...