【Android发展】它Fragment发展1
一直知道Fragment非常强大。可是一直都没有去学习,如今有些空暇的时间,所以就去学习了一下Fragment的简单入门。我也会把自己的学习过程写下来,假设有什么不足的地方希望大牛指正,共同进步。
一、Fragment简单介绍
1.Fragment作为Activity界面的一部分组成出现;
2.能够在一个Activity中同一时候出现多个Fragment,而且,一个Fragment亦可在多个Activity中使用;
3.在Activity执行过程中,能够加入、移除或者替换Fragment(add()、remove()、replace());
4.Fragment能够响应自己的输入事件,而且有自己的生命周期,当然。它们的生命周期直接被其所属的activity的生命周期影响。
那我们为什么要用Fragment呢?主要目的是用在大屏幕设备上--比如平板电脑上,支持更加动态和灵活的UI设计。平板电脑的屏幕要比手机的大得多,有很多其它的空间来放很多其它的UI组件,而且这些组件之间会产生很多其它的交互。我们能够把Fragment觉得是“小的Activity”。Fragment更加简洁。
二、Fragment的简单使用
那我们就简单的显示2个Fragment为例来解说一下。
1.在XML中加入Fragment:
新建Fragment1、Fragment2(注意:这里可能有2个包能够选择导入android.app.Fragment或android.support.v4.app.Fragment都是能够的,我这里选择使用了前者。可是两者使用时有差别的,在结尾中我会讲到):
Fragment1代码:
package com.example.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.fragmentdemo.R;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e("TAG", "in");
return inflater.inflate(R.layout.fragment1, container, false);
}
}
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; import com.example.fragmentdemo.R; public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e("TAG", "in");
return inflater.inflate(R.layout.fragment1, container, false);
}
}
Fragment2代码:
package com.example.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.fragmentdemo.R;
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, container, false);
}
}
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; import com.example.fragmentdemo.R; public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, container, false);
}
}
Fragment1的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:background="#FF69B4"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="这是第一个Fragment" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF69B4"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="这是第一个Fragment" /> </LinearLayout>
Fragment2的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:background="#EECBAD"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="这是第二个Fragment" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EECBAD"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="这是第二个Fragment" /> </LinearLayout>
我们在activity_main.xml中加入两个Fragment。代码例如以下:
<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.example.fragment.Fragment1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/fragment2"
android:name="com.example.fragment.Fragment2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false" > <fragment
android:id="@+id/fragment1"
android:name="com.example.fragment.Fragment1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" /> <fragment
android:id="@+id/fragment2"
android:name="com.example.fragment.Fragment2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" /> </LinearLayout>
MainActivity代码例如以下:
package com.example.fragmentdemo;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
import android.os.Bundle; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
然后执行project就能够显示Fragment了。以下是效果图。
2.动态加入Fragment:
我们仅仅须要改动MainActivity和activity_main.xml中的代码就能够了。
activity_main.xml代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</LinearLayout>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="vertical" > <LinearLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" /> </LinearLayout>
MainActivity代码:
package com.example.fragmentdemo;
import android.app.Activity;
import android.os.Bundle;
import com.example.fragment.Fragment1;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction()
.replace(R.id.main, new Fragment1()).commit();
}
}
import android.os.Bundle; import com.example.fragment.Fragment1; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction()
.replace(R.id.main, new Fragment1()).commit();
}
}
然后执行project就能够动态的显示Fragment了,以下是效果图。
三、app包下和V4包下的Fragment的差别
1、尽量不要用app包中的fragment。由于这个是在3.0之后才有的,支持的版本号太高,在低版本号中是是用不了的;
2、android.support.v4.app.Fragment:能够兼容到1.6的版本号。
3、关于这两个fragment使用<fragment>标签的问题:
(1)app.fragment和v4.fragment都是能够使用<fragment>标签的仅仅是在在使用的时候假设是app.fragment则没有什么特殊的地方继承Activity就可以。
(2)当v4.fragment使用<fragment>标签的时候就要特别注意了:当这个Activity的布局中有<fragment>标签的时候,这个Activity必须继承FragmentActivity,否则就会报错。
此时假设不卜继成FragmentActivity的话
编译系统会把<fragment>觉得是app包中的Fragment来处理。可是此时我们导入的是v4包中的FragmentAndroid官方文档中的Fragment的样例就是以app包中的Fragment来解说的。
(3)app包中关于Fragment的类和方法在V4包中都是有相应的相应的。
转载自:http://blog.csdn.net/a465456465/article/details/10415211,感谢。
上面就是Fragment的简单用法,Demo下载,下一节我会讲Fragment的具体使用。欢迎关注。我的博客园地址:http://www.cnblogs.com/getherBlog/p/3943547.html。
【Android发展】它Fragment发展1的更多相关文章
- Android开发环境的发展演变调研
Android开发环境的发展演变调研 前几年比较多的方法是用JDK+eclipse+ADT,该方法除了要配置JDK的路径之外, 还要在eclipse里面打开SDK Manage进行相应的操作.不过近两 ...
- android开发中fragment获取context
在用到fragment时无法使用.this来指定当前context内容,android开发中fragment获取context,可以使用getActivity().getApplicationCont ...
- Android Activity和Fragment的转场动画
Android Activity和Fragment的转场动画 Activity转场动画 Activity的转场动画是通过overridePendingTransition(int enterAnim, ...
- Android 动态创建Fragment
Fragment是activity的界面中的一部分或一种行为.可以把多个Fragment组合到一个activity中来创建一个多界面并且可以在多个activity中重用一个Fragment.可以把Fr ...
- Android碎片(Fragment)简述
碎片(Fragment)是一种可以嵌入活动当中的UI片段,它能让程序更加合理和充分地利用大屏幕的空间,因此碎片在平板上的应用非常广泛. 你可以将碎片理解成一个迷你型的活动,水平同样可能包含布局,同样都 ...
- Android中ViewPager+Fragment取消(禁止)预加载延迟加载(懒加载)问题解决方案
转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/53205878本文出自[DylanAndroid的博客] Android中Vie ...
- Android系列之Fragment(四)----ListFragment的使用
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- Android系列之Fragment(三)----Fragment和Activity之间的通信(含接口回调)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- Android系列之Fragment(二)----Fragment的生命周期和返回栈
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
随机推荐
- php实现从尾到头打印列表
php实现从尾到头打印列表 一.总结 4.数组倒序:array_reverse() 5.函数肯定要return,而不是echo 二.php实现从尾到头打印列表 输入一个链表,从尾到头打印链表每个节点的 ...
- html表单元素及表单元素详解
原文 https://www.jianshu.com/p/b427daa8663d 大纲 1.认识表单 2.认识表单元素 3.表单元素的分类 4.表单元素——文本框 5.表单元素button 6.表单 ...
- 【9204】第k小整数
Time Limit: 10 second Memory Limit: 2 MB 问题描述 现有n个整数,n≤10000,要求出这n个正整数中的第k个最小整数(相同大小的整数只计算一次),k≤1000 ...
- js进阶正则表达式实现过滤字符串(RegExp对象操作正则表达式)(正则:regular)(表达式:expression)
js进阶正则表达式实现过滤字符串(RegExp对象操作正则表达式)(正则:regular)(表达式:expression) 一.总结 1.str_replace:正则作用:高效快速匹配 2.break ...
- html 页面 黑白
css代码,写在最顶端 html {filter: progid:DXImageTransform.Microsoft.BasicImage(grayscale=1);-webkit-filter: ...
- Linux环境下Apache ActiveMQ 基本安装
原文链接:https://www.jianshu.com/p/1c017088aa95 在linux上安装mq,并映射到外网.1.Apache ActiveMQ安装基本条件请参考链接:2.下载Apac ...
- Android JNI编程(七)——使用AndroidStudio编写第一个JNI程序
版权声明:本文出自阿钟的博客,转载请注明出处:http://blog.csdn.net/a_zhon/. 目录(?)[+] 1.简单介绍一下NDK和JNI NDK:NDK是Native Develop ...
- 【u019】排序(sort)
[问题描述] 一个不同的值的升序排序数列指的是一个从左到右元素依次增大的序列,例如,一个有序的数列A,B,C,D 表示A<B,B<C,C<D.在这道题中,我们将给你一系列形如A< ...
- Android selector背景以及透明色
selector可以设置图片或layout的点击效果: <?xml version="1.0" encoding="utf-8"?> <sel ...
- 【9005】最短网络agrinet
Time Limit: 1 second Memory Limit: 256 MB 问题描述 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的 ...