Android应用开发学习笔记之Fragment
作者:刘昊昱
博客:http://blog.csdn.net/liuhaoyutz
Fragment翻译成中文就是“碎片”、“片断”的意思,Fragment通常用来作为一个Activity用户界面的一部分。例如,可以用Fragment1在左边显示一个列表,用Fragment2在右边显示选中列表项的详细内容。两个Fragment属于同一个Activity,并且每个Fragment有它自己的生命周期,可以处理它自己的用户输入事件,另外,Fragment还可以有自己的布局文件。在平板电脑等屏幕比较大的设备上,Fragment比较常用。
一、静态添加Fragment
下面我们来看一个使用静态方式添加Fragment的例子,其运行效果如下:
首先我们创建Fragment1的布局文件,其内容如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="This is fragment 1" /> </LinearLayout>
然后创建Fragment2的布局文件,其内容如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="This is fragment 2" /> </LinearLayout>
下面创建Fragment1的实现文件,其内容如下:
package com.liuhoayu; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class Fragment1 extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
} }
下面创建Fragment2的实现文件,其内容如下:
package com.liuhoayu; 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 { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, container, false);
} }
接下来实现主布局文件,使用Android:name导入前面创建的Fragment1和Fragment2。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false" > <fragment
android:id="@+id/fragment1"
android:name="com.liuhoayu.Fragment1"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" /> <fragment
android:id="@+id/fragment2"
android:name="com.liuhoayu.Fragment2"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" /> </LinearLayout>
最后,实现主Activity文件,其内容如下:
package com.liuhoayu; import android.app.Activity;
import android.os.Bundle; public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
二、动态添加Fragment
通过上面的例子,我们学会了怎样通过布局文件添加Fragment,下面我们来学习怎样动态的将Fragment添加到Activity中,实际上,动态添加Fragment更加灵活实用。
修改上一个例子中的主布局文件,将其中添加Fragment的语句去掉,这次我们将动态添加Fragment。修改后的主布局文件内容如下:
<?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:id="@+id/main_layout"
android:orientation="vertical" > </LinearLayout>
然后修改主Activity文件,因为没有在布局文件中静态添加Fragment,我们要在这里动态添加,其内容如下:
package com.liuhaoyu; import android.app.Activity;
import android.os.Bundle;
import android.view.Display; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Display display = getWindowManager().getDefaultDisplay();
if (display.getWidth() > display.getHeight()) {
Fragment1 fragment1 = new Fragment1();
getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment1).commit();
} else {
Fragment2 fragment2 = new Fragment2();
getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment2).commit();
}
} }
程序运行效果如下:
Android应用开发学习笔记之Fragment的更多相关文章
- Android应用开发学习笔记之播放音频
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android支持常用音视频格式文件的播放,本文我们来学习怎样开发Android应用程序对音视频进行操作. Andr ...
- android移动开发学习笔记(二)神奇的Web API
本次分两个大方向去讲解Web Api,1.如何实现Web Api?2.如何Android端如何调用Web Api?对于Web Api是什么?有什么优缺点?为什么用WebApi而不用Webservice ...
- Android应用开发学习笔记之事件处理
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android提供的事件处理机制分为两类:一是基于监听的事件处理:二是基于回调的事件处理.对于基于监听的事件处理,主 ...
- Android应用开发学习笔记之AsyncTask
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 在上一篇文章中我们学习了多线程和Handler消息处理机制,如果有计算量比较大的任务,可以创建一个新线程执行计算工作 ...
- [Android游戏开发学习笔记]View和SurfaceView
本文为阅读http://blog.csdn.net/xiaominghimi/article/details/6089594的笔记. 在Android游戏中充当主要角色的,除了控制类就是显示类.而在A ...
- Android应用开发学习笔记之菜单
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android中的菜单分为选项菜单(OptionMenu)和上下文菜单(Context Menu).通常使用菜单资源 ...
- Android应用开发学习笔记之Intent
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Intent是什么呢?来看Android官网上的定义: An intent is an abstractdescri ...
- Android应用开发学习笔记之多线程与Handler消息处理机制
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 和JAVA一样,Android下我们可以通过创建一个Thread对象实现多线程.Thread类有多个构造函数,一般通 ...
- Android应用开发学习笔记之BroadcastReceiver
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 一.BroadcastReceiver机制概述 Broadcast Receiver是Android的一种“广播发布 ...
随机推荐
- iOS WKWebview 网页开发适配指南
iOS WKWebview 网页开发适配指南 微信iOS客户端将于2017年3月1日前逐步升级为WKWebview内核,需要网页开发者提前做好网站的兼容检查和适配.如有问题,可参考文末联系方式,向我们 ...
- 【WIN10】VisualStateManager使用說明
Demo下載:http://yunpan.cn/cFjgPtWRHKH9H 访问密码 c4b7 顧名思義,視圖狀態管理器. 在WPF中,它的功能似乎更強大.在UWP中,閹割了GotElementSt ...
- thinkphp的_STORAGE_WRITE_ERROR_问题
今天服务器突然报这个问题(上图所示),在thinkphp的官网上也发现有朋友碰到这个问题,定位到应该是Runtime目录没有写权限,然后试着给Application下的Runtime目录 chmod ...
- 某谷 P5159 WD与矩阵
题面在这里 崴脚回家后的小休闲2333. 显然每一行的1的个数必须是偶数,这样可以归纳证明前i行异或出来的m位二进制数也有偶数个1,这样最后一行就有且仅有一种放法了. 于是ans = 2^((n-1) ...
- 【转载】实现UTF8与GB2312编码格式相互转换(VC)已经验证!
UTF-8编码:[1,1,1,0,A5,A6,A7,A8],[1,0,B3,B4,B5,B6,B7,B8],[1,0,C3,C4,C5,C6,C7,C8];对应的UNICODE编码:[A5,A6,A7 ...
- BZOJ 1022 [SHOI2008]小约翰的游戏John AntiNim游戏
1022: [SHOI2008]小约翰的游戏John Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1475 Solved: 932[Submit][ ...
- UVALive 5059 C - Playing With Stones 博弈论Sg函数
C - Playing With Stones Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu S ...
- PostgreSQL增强版命令行客户端(pgcli)
效果: 安装: https://www.pgcli.com/install 官网: https://www.pgcli.com/
- C++中使用REST操作
REST现在是非常流行的一种接口了,但对于C++这种古董语言来说,用起来并不很方便.无论是json操作还是http交互,用起来都比较麻烦. 如果你需要在c++中使用rest操作时,不妨试一下微软的cp ...
- oracle的dump理解
http://blog.csdn.net/notbaron/article/details/51228444 http://blog.csdn.net/lqx0405/article/details/ ...