【Android 开发教程】动态添加Fragments
fragment的真正用处是在程序运行过程中动态地添加。
1. 新建工程。
2. res/layout/main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal" >
- </LinearLayout>
3. res/layout/fragment1.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#00FF00"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/lblFragment1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="This is fragment #1"
- android:textColor="#000000"
- android:textSize="25sp" />
- </LinearLayout>
4. res/layout/fragment2.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#FFFE00"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="This is fragment #2"
- android:textColor="#000000"
- android:textSize="25sp" />
- </LinearLayout>
5. Fragment1.java
- public class Fragment1 extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // ---Inflate the layout for this fragment---
- return inflater.inflate(R.layout.fragment1, container, false);
- }
- }
6. Fragment2.java
- public class Fragment2 extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // ---Inflate the layout for this fragment---
- return inflater.inflate(R.layout.fragment2, container, false);
- }
- }
7. FragmentsActivity.java
- public class FragmentsActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- FragmentManager fragmentManager = getFragmentManager();
- FragmentTransaction fragmentTransaction = fragmentManager
- .beginTransaction();
- // ---get the current display info---
- WindowManager wm = getWindowManager();
- Display d = wm.getDefaultDisplay();
- if (d.getWidth() > d.getHeight()) {
- // ---landscape mode---
- Fragment1 fragment1 = new Fragment1();
- // android.R.id.content refers to the content
- // view of the activity
- fragmentTransaction.replace(android.R.id.content, fragment1);
- } else {
- // ---portrait mode---
- Fragment2 fragment2 = new Fragment2();
- fragmentTransaction.replace(android.R.id.content, fragment2);
- }
- // ---add to the back stack---
- fragmentTransaction.addToBackStack(null);
- fragmentTransaction.commit();
- }
- }
8. 调试。
【Android 开发教程】动态添加Fragments的更多相关文章
- Android开发之动态添加控件
动态添加TextView控件: 一:创建一个Android project项目 activity_main.xml文件: 1.用两个LinearLayout布局分别包裹一对TextView,EditT ...
- ArcGIS Runtime for Android开发教程V2.0(4)基础篇---MapView
原文地址: ArcGIS Runtime for Android开发教程V2.0(4)基础篇---MapView - ArcGIS_Mobile的专栏 - 博客频道 - CSDN.NET http:/ ...
- Android开发教程大全介绍
Android是由谷歌在2007年推出的一个开放系统平台,主要针对移动设备市场,目前版本为Android 4.0.Android基于Linux,开发者可以使用Java或C/C++开发Android应用 ...
- ArcGIS Runtime for Android开发教程V2.0(3)基础篇---Hello World Map
原文地址: ArcGIS Runtime for Android开发教程V2.0(3)基础篇---Hello World Map - ArcGIS_Mobile的专栏 - 博客频道 - CSDN.NE ...
- ArcGIS Runtime for Android开发教程V2.0(2)开发环境配置
原文地址: ArcGIS Runtime for Android开发教程V2.0(2)开发环境配置 - ArcGIS_Mobile的专栏 - 博客频道 - CSDN.NET http://blog.c ...
- ArcGIS Runtime for Android开发教程V2.0(1)基本概念
原文地址: ArcGIS Runtime for Android开发教程V2.0(1)基本概念 - ArcGIS_Mobile的专栏 - 博客频道 - CSDN.NET http://blog.csd ...
- 收集整理Android开发所需的Android SDK、开发中用到的工具、Android开发教程、Android设计规范,免费的设计素材等。
AndroidDevTools Android Dev Tools官网地址:www.androiddevtools.cn 收集整理Android开发所需的Android SDK.开发中用到的工具.An ...
- Android开发教程AnimationDrawable逐帧播放动画
下面我们一起来看篇Android开发AnimationDrawable控制逐帧播放动画实现过程,希望文章对各位朋友带不一些帮助. 当我们点击按钮时,该图片会不停的旋转,当再次点击按钮时,会停止在当前的 ...
- Android中如何动态添加碎片
Android中的开发需要兼容手机和平板,两个方面.这就引入了碎片的概念.(注意:这里用的Fragment强烈建议使用support-v4库中的Fragment) 碎片:是一种可以嵌入在活动当中的UI ...
随机推荐
- (1)Python 安装使用IDLE
安装 官网 https://www.python.org/ Windows x86 web-based installer 在线安装 Windows x86 executable installer ...
- 20180824Noip模拟赛10分总结
嗯,总之,是我太傻了. 我真傻,真的,我单知道最小生成树,却不知道还有最大生成树 T1 最大生成树.... 累加每一个环内,最大生成树的边权,(对环求最大生成树,则必然剩下一个边权最小的边(因为是求生 ...
- hdu6156
hdu6156 题意 求 \([2, 36]\) 进制下,给定区间内的数是回文数的个数.每存在一个回文数,答案加上该回文数的进制. 分析 10进制下回文数是 数位DP 很常见的问题,这道题只需要把在转 ...
- [Contest20180314]数列
数据范围告诉我们要写两档的分 第一档:$M\leq200,N\leq10^9$,可以枚举$m$计算答案 直接矩阵快速幂:$O\left(M^4\log_2N\right)$,会超时,所以我们需要某些“ ...
- 【计算几何】【凸包】bzoj2829 信用卡凸包
http://hzwer.com/6330.html #include<cstdio> #include<cmath> #include<algorithm> us ...
- 【并查集】bzoj2054 疯狂的馒头
因为只有最后被染上的颜色会造成影响,所以倒着处理,用并查集维护已经染色的区间的右端点,即fa[i]为i所在的已染色区间的右端点,这样可以保证O(n)的复杂度. #include<cstdio&g ...
- 【状态压缩DP】BZOJ1087-[SCOI2005]互不侵犯King
[题目大意] 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. [思路] 先预处理每一行可行的状态 ...
- python3开发进阶-Django框架的详解
一.MVC框架和MTV框架 MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分: 模型(Model).视图(View)和控制器(Con ...
- mysql replication 复制的一些问题
1 过大的复制延迟 mysql 的复制延迟是一个常见问题,现在已经有一些解决方案,如淘宝开发的一些工具 2 没有磁盘空间 复制导致磁盘空间塞满,二进制日志.中继日志或临时文件把磁盘塞满,slave ...
- “自适应”高度的 textarea 文本输入框
写在前面 那啥,在我的那个很安静的一个 CSS 群(群号:82991297)突然看到有人在问一个问题. 使用 css 如何实现:textarea 如何实现高度自适应? 当时看到这个问题的时候,我脑中只 ...