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=" ...
随机推荐
- 498. Diagonal Traverse对角线z型traverse
[抄题]: Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in dia ...
- 《Java程序设计》第二周学习总结(一)
教材学习内容总结 标识符.关键字 基本数据类型 类型转换运算 输入.输出数据 数组 教材学习中的问题和解决过程 问题1:在创建vim并编写完成后出现以下情况 问题1解决方案:打开虚拟机的文件目录,发现 ...
- MySQL优化(四) 慢查询的定位及优化
一.SQL语句优化的一般步骤: (1)通过 show status 命令了解各种 SQL 的执行效率: (2)定位执行效率较低的 SQL 语句(重点是 Select): (3)通过 explain 分 ...
- each遍历
<script> $(function () { $.each([52, 97], function(index, value) { alert(index + ': ' + value) ...
- mysql查询数据
select column,column from table where clause [limit n] [offset]; 查询语句中你可以使用一个或者多个表,表之间使用逗号(,)分割,并使用W ...
- python 安装第三方包时 read timed out
记录下安装python第三方包超时报错,解决方法:(以安装numpy为例) pip install numpy 报错:raise ReadTimeoutError(self._pool, None, ...
- 【轻松前端之旅】CSS选择器中的空格与尖括号有何区别?
CSS选择器中的空格与尖括号有何区别? 例子1: .a .b { margin: 0; } 空格隔开a和b,选择所有后代元素. 例子2: .a>.b { margin: 0; } 尖括号隔开a和 ...
- MYSQL 开发总结
1.mysql中,VARCHAR(N)中的N代表的是字符数,而不是字节数.例如VARCHAR(255)表示可以保存255的中文 2.过大的长度会消耗更多的内存.VARCHAR(N),存储时是按照数据实 ...
- PowerShell工作流学习-1-嵌套工作流和嵌套函数
关键点: a)嵌套深度没有任何语法限制,但是嵌套三个层次的工作流不支持任何通用参数,包括工作流通用参数 b)嵌套工作流可以调用当前范围和任何父范围内的工作流和函数 c)工作流不允许递归调用,脚本和函数 ...
- bash编程-sed
sed(Stream Editor)是Linux系统下的一个文本流编辑器,它将文本文件内容逐行读取到标准输出,并将此行内容写入模式空间(pattern space),然后按照给定的地址定界和命令处理匹 ...