Android-Fragment(1)简单实现
来源:互联网 发布:node fs模块 编辑:程序博客网 时间:2024/06/05 03:13
1. 前言
关于fragment
- 它是一个区域的UI,可以绑定自己的Layout。当然,他也可以不在界面中显示,而在后台工作。
- 我们可以在一个Activity里加入多个Fragment,也可以多个Activity重用一个Fragment。
- 它拥有自己的生命周期,同时被它所寄存的Activity的生命周期所影响。当Activity销毁,Fragment也被销毁,当Activity暂停,Fragment也暂停
- Activity正运行时,我们可以对Fragment进行添加,删除,替换等操作
2. 实现步骤
- 在main_activity.xml中添加Fragment的layout,跳出提示框:建立继承自Fragment的子类,
选择Create New 在类中添加必要Fragment必要的生命周期方法
OnCreateView
:在Fragment第一次绘制用户界面,调用此方法,传入它所对应的layoutOnCreate
:创建Fragment调用此方法OnPause
:用户离开调用
这里我只是简单地实现OnCreateView方法,将Layout绑定
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.first,container,false); }
3. 例子
- first.xml
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this is fragment" /></FrameLayout>activity_main.xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.fiest.MainActivity" > <fragment android:id="@+id/fragment1" android:name="com.example.fiest.first" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginLeft="88dp" android:layout_marginTop="172dp" android:layout_toRightOf="@+id/textView1" /></RelativeLayout>
First.Activity
import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class first extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.first,container,false); }}
到这里,基本就差不多了,下一篇讲述如何在Activity里实现Fragment的创建,删除。
0 0
- Android-Fragment(1)简单实现
- Fragment(1)-简单实现
- Android Fragment学习笔记(1)———简单Fragment实现
- Android安卓Fragment简单实现
- Android中Viewpage+Fragment的简单实现
- Android Fragment懒加载简单实现
- Android Fragment懒加载简单实现
- Android Fragment简单使用
- Android Fragment 简单实例
- android ---Fragment简单示例
- Android---静态fragment简单
- Android简单Fragment切换
- android ViewPager Fragment实现微信qq简单滑动过程
- android 用fragment实现简单菜单点击功能
- Android简单有效Viewpager+Fragment实现滑动标签页
- Android开发笔记之ViewPager+Fragment简单列子的实现
- Android学习日记(yzy):Fragment与VierPager简单实现
- Android例子—Fragment实例讲解—新闻(购物)类App列表Fragment的简单实现
- 函数中的私有变量和特权方法
- Python调用OpenCV的接口
- <Effective Mordern C++>笔记:Item 1: Understand the template type deduction.
- ROS 学习系列 -- 树莓派编译安装完整版本ROS
- 利用锚点制作简单索引效果
- Android-Fragment(1)简单实现
- 文章标题
- AngularJS内置服务器--$http服务的简单用法
- 硬件常用术语
- 登录功能
- HDU 1233 还是畅通工程(最小生成树 Prim+Kruskal)
- javascript严格模式下的8点规则
- JAVA对数计算
- 第十一周(Dynamic ProgrammingV)