【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 ...
随机推荐
- 操作Image,封装的方法
using System; using System.Collections; using System.IO; using System.Drawing; using System.Drawing. ...
- POJ 1321 棋盘问题【DFS/回溯/放与不放/类似n皇后】
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 62164 Accepted: 29754 Description 在一 ...
- CentOS5.5下直接安装MySQL5.6
1.安装平台:CentOS5.5 2.由于从MySQL5.0升到MySQL5.6出现重大问题,为了不影响进度,暂时先直接安装MySQL5.6 2.1首先确认yum源,这里使用http://repo.m ...
- 【2-SAT】URAL - 2089 - Experienced coach
题意:给出n对点a,b 要求从没对点中选出一个,且最终选出的点n个数不能存在相同的.输入数据满足每种数最多出现3次,最少出现1次 思路:第i对点的编号2*i, 2*i+1, 因为每个数最多出现3 ...
- 【博弈论】【SG函数】【枚举】bzoj1188 [HNOI2007]分裂游戏
因为第i个瓶子里的所有豆子都是等价的,设sg(i)表示第i个瓶子的sg值,可以转移到sg(j)^sg(k)(i<j<n,j<=k<n)的状态. 只需要考虑豆子数是奇数的瓶子啦, ...
- (转)关于Unity3D的编辑器崩溃时的线索定位
今天在Unity3D编辑器中进行功能测试的时候,编辑器突然崩溃了(就是整个窗口突然消失,进程直接结束)之后也没有任何错误报告信息提示.好吧,应该是偶现问题,我侥幸地想,我用的好歹也是正版啊,不应该总出 ...
- Eclipse / Pycharm | 使用过程中的一些问题笔记
最近有比较多的用到这两款工具,其中也遇到一些问题,知道了一些快捷键 快捷键什么的这里就不讲了,去网上搜搜,经常使用下,自然就熟悉了 主要记录一下我遇到的几个问题 文章目录 Pycharm出现的部分快捷 ...
- httpclient4.3访问https
1.创建一个访问https的工具类 package org.aaa.tool;import java.io.File; import java.io.IOException; import java. ...
- Xcode8 不能显示blame,show blame for line 灰色不可点解决办法
1.原因 创建工程时没勾选create git repository 2.解决办法 ➜ ~ cd /Users/zhanglinfeng/Documents/EastMoney/EMLive / ...
- JAVA 按时间排序
排序使用的是 Collections.sort(List,Comparator) 自定义类实现Comparator接口 假如A的值大于B,你返回1.这样调用Collections.sort()方法就是 ...