119_动态创建fragment笔记
1. fragment优点:
- 支持动态创建
- 可以规避Android系统对Activity规则的限制(比如可以减少AndroidManifest.xml的文件长度)
2. fragment托管给activity的方法:
2.1 添加fragment到activity布局中:
布局文件:activity_mail.xml:
里面有两个fragment元素,注意的是fragment的name属性指明了fragment的类的名称,该类处理所有fragment事件的响应。
<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"tools:context=".MainActivity" ><fragment
android:id="@+id/fragment1"android:name="com.itheima.fragment.Fragment1"android:layout_width="0dip"android:layout_height="match_parent"android:layout_weight="1" /><fragment
android:id="@+id/fragment2"android:name="com.itheima.fragment.Fragment2"android:layout_width="0dip"android:layout_height="match_parent"android:layout_weight="1" /></LinearLayout>
布局文件fragment1.xml:
<?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" ><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="我是fragment1的textview" /></LinearLayout>
布局文件fragment2.xml:
<?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="#00ff00"android:orientation="vertical" ><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="我是fragment2的textview" /></LinearLayout>
MainActivity.java:
package com.itheima.fragment;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}
}
Fragment1.java:
package com.itheima.fragment;import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;/*** 类似于小的activity* activity需要在清单文件配置 * fragment 可以在布局文件配置*/
public class Fragment1 extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment1, container, false);}
}
Fragment2.java:
package com.itheima.fragment;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 { @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment2, container, false);}
}
运行效果:
2.2 在activity代码中添加fragment:
activity_main.xml中删除fragment的定义
<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"tools:context=".MainActivity" >
</LinearLayout>
MainActivity.java中动态加载fragment
android.R.id.content是当前activity的界面
package com.itheima.dynamicallyfragment;import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.WindowManager;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);WindowManager wm = getWindowManager();int width = wm.getDefaultDisplay().getWidth();int height = wm.getDefaultDisplay().getHeight();// 帧片段服务的管理者FragmentManager fm = getFragmentManager();FragmentTransaction fragmentTransaction = fm.beginTransaction();if (height > width) {// 竖直屏幕fragmentTransaction.replace(android.R.id.content, new Fragment1());} else {// 水平的屏幕fragmentTransaction.replace(android.R.id.content, new Fragment2());}fragmentTransaction.commit();}}
默认显示:
ctrl+F12改变模拟器朝向
119_动态创建fragment笔记相关推荐
- android module中获取 app_Android组件化架构 - 4. 动态创建
Android 组件化中使用动态创建的作用是解耦: 1. 反射机制 反射有两个作用:1.反编译:.class->.java;2.通过反射机制访问java对象中的属性,方法,构造器等: 实现反射, ...
- 深入浅出mfc学习笔记——六大关键技术之仿真_运行时和动态创建
1:PS88:MFC的类层次结构 <1>CObject <2>CCmdTarget,CDocument <3>CCmdTarget_CWinThread_CWinA ...
- FreeRTOS学习笔记---动态创建任务 xTaskCreate() 源码分析
在看FreeRTOS源码的时候,各个函数相互调用,各种参数相互传递,看的人云里雾里,越看越糊涂.为了搞清楚各个函数之间的相互关系,就边看源码,边画思维导图,用文字将函数功能描述出来,搞清楚整个函数框架 ...
- Python基础笔记_Day12_Python元类、type动态创建类、Python动态创建方法、Python运算符、Python发邮件、短信
Day12_Python元类.type动态创建类.Python动态创建方法.Python运算符重载.Python发邮件.短信 12.01_Python语言基础(类对象)(熟悉) 12.02_Pytho ...
- android fragment动态加载,动态添加Fragment
8种机械键盘轴体对比 本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选? 动态添加Fragment 在程序运行时, 根据具体情况来动态地添加Fragment到Activity中. 1 新建 An ...
- android 微信分享gif图,android后台动态创建图片并实现微信分享
今天就记录一下. 先说明一下,之前没有做过类似的东西,百度了一两天才知道,说来很惭愧.有点笨,只能这样说. 在我的脑里只明白,如果要动态创建图片: 一.就是new 嘛 二.就是LayoutInflat ...
- C语言之动态创建链表的两种方式
之前我们说的链表是基于静态创建的,但静态创建的链表很不灵活而且在工作中的项目中的很多场景都不适合,所以我们来记录一下链表的两种动态创建的方式. 1.方式一:链表动态创建之头插法 那么我们直接上代码讲: ...
- Unity动态创建材质球
目录 一.获取贴图 1:从Assets文件中获取 2:从本地文件中获取 二.创建材质球 三.替换材质 总结 前言 我们在做一些AR类似与涂涂乐的功能时,经常会用到给模型替换材质球的功能.当我们的用户创 ...
- python创建类的实例方法-Python中动态创建类实例的方法
简介 在Java中我们可以通过反射来根据类名创建类实例,那么在Python我们怎么实现类似功能呢? 其实在Python有一个builtin函数import,我们可以使用这个函数来在运行时动态加载一些模 ...
最新文章
- 管理系统中计算机应用 重点章节,11年《管理系统中计算机应用》 第5章 重点要点.doc...
- 上传一个 游戏server架构图
- Linux之ab命令
- 面料经纬向、正反面判别方法
- 让memcached和mysql更好的工作
- 【java学习之路】(java SE篇)003.java SE基础语法之数组
- 快点来学吧!Spring事务是如何传播的?快来收藏!
- mac软件推荐,支持m1(持续更新)
- 已知文件url,批量下载文件
- ip地址聚合-路由聚合
- 微波射频学习笔记18-------偶极子天线和微波天线设计介绍
- Automate your Android app testing
- App自动化测试怎么做?实战分享App自动化测试全流程
- 表格比手机屏幕宽时不压缩,可左右滚动,格子内容不换行
- java 国际化_Java国际化基础
- 关于QA QE QC 测试职位的区别
- AIBU-在建工程转固定资产(预转固)报错:消息号AW002 资产无单项需结算
- 25. 获取员工其当前的薪水比其manager当前薪水还高的相关信息
- 2021象山中学高考成绩查询,2019年象山中学高考喜报、二本上线人数1424人
- 【数值分析】分别使用复合梯形公式和复合辛普森公式计算如下积分(python)
热门文章
- Fluke 17B+ 万用表与PC通讯(数据实时采集)-- 升级篇
- 联发科mtk手机处理器怎么样_realme发布新品;中兴AXON 20 5G手机首发屏下摄像头;联发科发布Helio G95处理器...
- Databricks 第10篇:Job
- 为什么局域网网段不同不能通信
- 学习matlab(五)——多项式、插值、极限
- python——Socket网络编程(详细讲解)(一)
- 教育局网站群建设思路
- more exceptional c++简要笔记
- 波音发布小巧型无人直升机,可一次载重227千克
- 互联网头条:寒冬,你们全家都寒冬