Android学习笔记2:Activity(二)—— Fragments
来源:互联网 发布:faker vs ryu知乎 编辑:程序博客网 时间:2024/06/11 22:15
昨天貌似CSDN的博客登不上,所以没更新,也顺带的休息了一天 O(∩_∩)O
今天我们来研究下Fragments,这个模块貌似用的不多,因为在好多书上都没提及,不过官方放出来了,应该就有其用意,既然这个系列叫做“研习”,自然就不能忽略了。
该模块使用版本最低为Android3.0(API level 11)
Fragments翻译的意思为“片段”,官方给出的解释为"A Fragmemts
represents a behavior or a portion of user interface in anActivity"
。我们可以理解成他是组成Activity的一部分,他拥有自己独立的生命周期(lifecycle)、事件(events)。
虽然Fragments拥有自己的生命周期,但它的生命周期还制约与它所嵌入的Activity,如果Activity销毁了,那Fragments也就不存在了。
Frgments适用于平板电脑(貌似3.0就是为tablet而生的),其目的是为了在一个屏幕内看到更多的信息,就好比网页中的两列布局,点击左侧的菜单,在右侧展示新的内容,见下图
创建Fragment
创建一个Fragment,需要建立一个该类继承Fragment类的子类和一个布局文件
public static class ExampleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.example_fragment, container, false); }}
在onCreateView()方法中设置Fragment的布局文件。
当然除了继承Fragment类,还能继承DialogFragment、ListFragment和PreferenceFragment类,这三个类也都继承了Fragment类
在Activity中加入Fragment
在Activity中加入Fragment最方便的方法,就是在Activity布局文件中直接加入Fragment的布局文件,下面就是一个例子
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:name="com.example.news.ArticleListFragment" android:id="@+id/list" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment android:name="com.example.news.ArticleReaderFragment" android:id="@+id/viewer" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" /></LinearLayout>
Fragment的生命周期
Fragment的生命周期其实和Activity差不多,以下是官方提供的一张对比图
其实Fragment的内容官方给的还是挺多的,最后也给了个例子。本来想写的详细,但觉得那好像在翻译文章,但想言简意赅,貌似又初写博文,没那水平,所以就将就着写点重点和心得。以后有了更深入的理解再补充……
- Android学习笔记2:Activity(二)—— Fragments
- Android学习笔记10——用户界面之Fragments(2)
- Android学习笔记9——用户界面之Fragments(1)
- Android学习笔记11——用户界面之Fragments(3)
- android学习笔记之Fragments
- Android学习笔记(二)---Activity
- Android学习笔记二:Activity
- 学习笔记(二)Activity
- Android学习笔记(二)活动(Activity)
- Android学习笔记(二)第一个Activity
- Activity 生命周期(二)-- MarsChen Android 开发教程学习笔记
- Android学习笔记之----Activity(二)LaunchMode
- Android学习笔记:(二)两个activity跳转
- Android学习笔记:<二>01.Activity初步
- Android学习第8课—Activity生命周期(二)
- android学习二(Activity)
- Android API Guides学习4——Fragments
- Android 管理Activity中的fragments
- OpenCV矩阵结构CvMat初探
- 数学建模十大经典算法漫谈
- 记录一下自己开发环境(centos6.2)的配置
- PE文件和COFF文件格式分析——签名、COFF文件头和可选文件头3
- 怎么在虚拟机中安装apk软件
- Android学习笔记2:Activity(二)—— Fragments
- android创建文件夹和文件
- 切身体会设计系统时的前瞻性问题
- 深入理解Flash Player的安全域(Security Domains)
- 深入理解Flash Player的应用程序域(Application Domains)
- Win7下虚拟机个人使用小结:Virtual PC,VMware和VirtualBox
- 通过JS提交表单
- YUV格式详解
- wget 命令