Android Fragment用法详解(1)--静态使用Fragment
先说明一下,本例子是运行在Android Studio下面的。同样的代码复制粘贴到Eclipse运行却会报错。具体原因我也没有细查。知道的哥们,留言通知下呗。
Fragment,也就是碎片,本意是为了适配大屏幕的安卓设备而生的。但是出现后,很多安卓开发者都非常喜欢这个东西。这个东西很好用,但是也不是很容易用。下面我来来细细解说Android中的Fragment。
1、Fragment产生的缘由
运行Android的设备繁多,屏幕大小更是多种多样。针对不同屏幕尺寸,通常情况下,开发者都是先针对手机开发一套源代码,然后拷贝一份,修改布局以适应大屏幕设备,或平板,电视等。为了决解这样的麻烦,Google推出了Fragment。你可以把Fragment当成Activity的一个界面的一个组成部分,甚至Activity的界面可以完全有不同的Fragment组成,Fragment拥有自己的生命周期和接收、处理用户的事件,这样就不必在Activity写一堆控件的事件处理的代码了。更为重要的是,你可以动态的添加、替换和移除某个Fragment。
2、Fragment的生命周期
Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期。官网这张图很好的说明了两者生命周期的关系:
可以看到Fragment比Activity多了几个额外的生命周期回调方法:
onAttach(Activity)
当Fragment与Activity发生关联时调用。
onCreateView(LayoutInflater, ViewGroup,Bundle)
创建该Fragment的视图
onActivityCreated(Bundle)
当Activity的onCreate方法返回时调用
onDestoryView()
与onCreateView想对应,当该Fragment的视图被移除时调用
onDetach()
与onAttach相对应,当Fragment与Activity关联被取消时调用
注意:除了onCreateView,其他的所有方法如果你重写了,必须调用父类对于该方法的实现,
3、静态的使用Fragment
这是使用Fragment最简单的一种方式,把Fragment当成普通的控件,直接写在Activity的布局文件中。步骤:
1、继承Fragment,重写onCreateView决定Fragemnt的布局
2、在Activity中声明此Fragment,就当和普通的View一样
下面展示一个例子(我使用2个Fragment作为Activity的布局,一个Fragment用于标题布局,一个Fragment用于内容布局):
TitleFragment的布局文件:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:background="@android:color/darker_gray"
- android:layout_height="45dp">
- <ImageButton
- android:id="@+id/im_button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@android:drawable/star_on"
- />
- <TextView
- android:textSize="25sp"
- android:textColor="@android:color/holo_red_dark"
- android:gravity="center"
- android:text="Fragment制作标题栏"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </RelativeLayout>
ContentFragment布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:gravity="center"
- android:layout_height="match_parent">
- <TextView
- android:gravity="center"
- android:textSize="25sp"
- android:text="Fragment当作主面板"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- </RelativeLayout>
MainActivity布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <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"
- >
- <fragment
- android:id="@+id/fg_title"
- android:layout_width="match_parent"
- android:layout_height="45dp"
- android:name="app.linfeng.com.myapplication.TitleFragment"
- />
- <fragment
- android:layout_below="@id/fg_title"
- android:id="@+id/fg_content"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:name="app.linfeng.com.myapplication.ContentFragment"
- />
- </RelativeLayout>
TitleFragment源代码
- package app.linfeng.com.myapplication;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ImageButton;
- import android.widget.Toast;
- public class TitleFragment extends Fragment {
- private ImageButton im_button;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.title_fragment_layout,container,false);
- im_button = (ImageButton) view.findViewById(R.id.im_button);
- im_button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(getContext(),"业务逻辑写在Fragment上,Activity是不是很整洁了?",Toast.LENGTH_SHORT).show();
- }
- });
- return view;
- }
- }
ContentFragment源代码
- package app.linfeng.com.myapplication;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- public class ContentFragment extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.content_fragment_layout,container,false);
- return view;
- }
- }
MainActivity源代码
- package app.linfeng.com.myapplication;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- /**
- * 因为Android Studio新建Activity是自动继承AppCompatActivity,所以我也没有改成
- * Activity,这个和本案例没有关系哈。
- */
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // requestWindowFeature(Window.FEATURE_NO_TITLE);
- //因为继承的是AppCompatActivity,所以requestWindowFeature()失效了
- getSupportActionBar().hide();
- setContentView(R.layout.activity_main);
- }
- }
最后我们看看运行效果
静态使用Fragment其实就是把Fragment当成普通的View一样声明在Activity的布局文件中,然后所有控件的事件处理等代码都由各自的Fragment去处理,瞬间觉得Activity好干净有木有~~代码的可读性、复用性以及可维护性是不是瞬间提升了
Android Fragment用法详解(1)--静态使用Fragment的更多相关文章
- Android Fragment用法详解(2)--动态添加Fragment
在上一篇文章<Android Fragment用法详解(1)--静态使用Fragment>我们讲解了Fragment的最简单的用法.这次我们来说一说Fragment复杂一丢丢的用法.在代码 ...
- Android GLSurfaceView用法详解(二)
输入如何处理 若是开发一个交互型的应用(如游戏),通常需要子类化 GLSurfaceView,由此可以获取输入事件.下面有个例子: java代码: package eoe.ClearTes ...
- Android.mk用法详解
一.Android.mk介绍 Android.mk是Android提供的一种makefile文件,用来指定诸如编译生成so库名.引用的头文件目录.需要编译的.c/.cpp文件和.a静态库文件等.要掌握 ...
- android: startActivityForResult用法详解
一.如果想在Activity中得到新打开Activity 关闭后返回的数据,需要使用系统提供的startActivityForResult(Intent intent, int requestCode ...
- Android getevent用法详解
getevent 指令用于获取 input 输入事件,比如获取按键上报信息.获取触摸屏上报信息等. 指令源码路径:/system/core/toolbox/getevent.c getevent -h ...
- Class.forName()用法详解
Class.forName()用法详解 标签: classjvmjdbc数据库documentationjava 2012-03-29 09:39 40414人阅读 评论(8) 收藏 举报 分类: ...
- Android之canvas详解
首先说一下canvas类: Class Overview The Canvas class holds the "draw" calls. To draw something, y ...
- 【转】Android Canvas绘图详解(图文)
转自:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1212/703.html Android Canvas绘图详解(图文) 泡 ...
- Android编译过程详解(一)
Android编译过程详解(一) 注:本文转载自Android编译过程详解(一):http://www.cnblogs.com/mr-raptor/archive/2012/06/07/2540359 ...
随机推荐
- IDEA运行debug为灰色无法运行
这种情况一般在使用他人项目的时候产生. 检查几个问题,本地环境是否配置好 然后进入项目的入口右键运行是否可以正常运行 如果不可以,就在 修改一下src文件 一般就可以正常使用了
- Java日期时间输出格式优化
使用printf格式化日期 printf 方法可以很轻松地格式化时间和日期.使用两个字母格式,它以 %t 开头并且以下面表格中的一个字母结尾. 转 换 符 说 明 示 例 c 包括全部 ...
- js备忘录模式
备忘录(Memento):在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. 备忘录模式比较适用于功能比较复杂的,但需要维护或记录属性 ...
- Spring Boot入门——邮件发送
1.引入依赖 <!-- mail依赖 --> <dependency> <groupId>org.springframework.boot</groupId& ...
- SQL Server中的执行引擎入门
简介 当查询优化器(Query Optimizer)将T-SQL语句解析后并从执行计划中选择最低消耗的执行计划后,具体的执行就会交由执行引擎(Execution Engine)来进行执行.本文旨在 ...
- WPF各种控件详解——(WPF从我炫系列)
http://blog.csdn.net/zx13525079024/article/details/5694638
- thinkphp框架的优缺点
ThinkPHP的优缺点如下: 1.高级模型:可以轻松支持序列化字段.文本字段.只读字段.延迟写入.乐观锁.数据分表等高级特性. 2.视图模型:轻松动态地创建数据库视图,多表查询相对简单. 3.关联模 ...
- MySQL 基础理论面试题整理
前言: 之前整理公司面试题的时候,看了一篇大神些 SQL 优化之六脉神剑 文章,写的真好! 博主有一些 MySQL 的面试题,简单抽了一个备注一下,补充一下自己不熟悉的地方. 一.在MySQL中, ...
- application.yml配置log日志
#日志文件的配置logging: pattern: console: "%d - %msg%n" file: /var/log/sell.log 注解@Slf4j
- 【Prism】MEF版Commanding
引言 接下来的是Commanding Demo的改造. DelegateCommand WPF本身提供了一个RoutedCommand,然而没什么卵用.在Prism框架中提供了个更人性化的ICo ...