Fragment 基础详解
来源:互联网 发布:管道安装设计软件 编辑:程序博客网 时间:2024/05/21 04:43
在Android3.0时入了Fragment概念,主要目的是用在大屏幕
设备上--例如平板电脑上,支持更加动态和灵活的UI设计。
平板电脑的屏幕要比手机大得多,有更多的空间来放更多的
UI组件,并且这些组件之间可以产生更多的交互。
Fragment在你的应用中应当是一个模块化和可重用的组件,
因为Fragment定义了它自己的布局,以及通过使用它自己的
生命周期回调方法定义了它自己的行为,你可以将Fragment
包含到多个Activity中。
- Fragment可以作为Activity界面的一部分组成出现
- 可以再一个Activity中同时出现多个Fragment,并且一个
Fragment也可以在多个Activity中使用
3. 在Activity运行过程中,可以添加、移除和替换Fragment
4. Fragment你可以相应自己的输入事件,并且有自己的生命
周期,他们的生命周期受宿主Activity的生命周期影响
-----------------------------------------------------------------------------------
静态加载方法:
首先创建mainActivity2 和 myFragment 这两个类,并且让mainActivity2
继承Activity实现OnCreate方法,让myFragment 继承Fragment并且
实现OnCreateView方法,再分别添加main2.xml文件和Fragment.xml
布局文件。其中main2.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:orientation="vertical" >
<fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
//必须要写Fragment的属性id 和name
android:id="@+id/fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
//绑定java类
android:name="com.example.fragmenttest.MyFragment"
/>
</LinearLayout>
android:name="com.example.fragmenttest.MyFragment"
/>
</LinearLayout>
---------------------------------------------------------------------------------------
很明显在main2中包含了一个Fragment,这个Fragment可以在
myFragment 中实现布局。
myFragment部分代码:
------------------------------------------------------------------------------------------
public class MyFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
//layout文件转换成View对象
/**
* resource:Fragment需要加载的布局文件*
* root:加载layout的父ViewGroup*
* attactToRoot:fakse,不返回父ViewGroup
*/
View view= inflater.inflate(R.layout.fragment, container,false);
TextView tv = (TextView) view.findViewById(R.id.textView1);
tv.setText("通过静态加载Fragment");
return view;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
//layout文件转换成View对象
/**
* resource:Fragment需要加载的布局文件*
* root:加载layout的父ViewGroup*
* attactToRoot:fakse,不返回父ViewGroup
*/
View view= inflater.inflate(R.layout.fragment, container,false);
TextView tv = (TextView) view.findViewById(R.id.textView1);
tv.setText("通过静态加载Fragment");
return view;
}
}
*-------------------------------------------------------------------------------------
现在已经基本实现了,只要在mainActivity的按钮监听器进行判断,
并且用Intent去转换,即可实现
注意不要忘记在Mainifest.xml文件中声明mainactivity2
动态加载
---------------------------------------------------------------------------------------.
//自定义的类,继承Fragment
MyFragment2 fragment= new MyFragment2();
//创建Fragment管理者
FragmentManager fm= getFragmentManager();
FragmentManager fm= getFragmentManager();
//获取管理者的beginTransaction对象
android.app.FragmentTransaction bts = fm.beginTransaction();
android.app.FragmentTransaction bts = fm.beginTransaction();
//通过add方法添加
bts.add(R.id.Layout1,fragment);
bts.commit();
bts.add(R.id.Layout1,fragment);
bts.commit();
0 0
- Fragment 基础详解
- 【Android基础】Fragment 详解之Fragment介绍
- 【Android基础】Fragment 详解之Fragment生命周期
- Fragment(碎片)基础详解【1】
- Android基础使用之Fragment详解一
- Android基础——Fragment详解
- Fragment基础
- fragment基础
- fragment基础
- fragment基础
- Fragment基础
- Fragment基础
- Fragment基础
- Fragment基础
- Fragment基础
- Android基础之Fragment与Activity交互详解
- Android基础之Fragment与Activity交互详解(二)
- Android基础之Fragment与Activity交互详解
- X11
- 多重继承
- Android控件之ProgressBar
- 剑指offer之面试题:查找和排序
- Android控件之WebView
- Fragment 基础详解
- Struts中错误: No result defined for action
- 116. Populating Next Right Pointers in Each Node
- 实现Fragment与Activity通信
- Potato项目文档(3)
- Android之 Viewpager
- 258.[LeetCode]Add Digits
- 【Leetcode】:242. Valid Anagram问题 in Go语言
- Viewpager使用FragmentAdapter