Fragment之一:Fragment入门
参考自张泽华视频
Fragment是自Android3.0后引入的特性,主要用于在不同的屏幕尺寸中展现不同的内容。
Fragment必须被嵌入Activity中使用,总是作为Activity的组成部分。
简单示例:
一个Activity的界面由2个部分组成,每个部分分别是一个Fragment。
效果图如下:
1、创建第一个Fragment的界面文件。
Fragment的界面文件和一般的Activity布局文件一样,如。
<?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="#0000ff"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/fragment1"
/> </LinearLayout>
2、创建第一个Fragment的类文件。
package com.ljh.fragmentdemo; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; //继续Fragment类。
public class Fragment1 extends Fragment { @Override
//在Fragment被加载时调用
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//加载某个布局文件。
return inflater.inflate(R.layout.fragment1, null);
}
}
3、创建第二个Fragment的界面文件。
<?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="#00ff00"
android:orientation="vertical" > <TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/fragment2"/> </LinearLayout>
4、创建第二个Fragment的类文件。
package com.ljh.fragmentdemo; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; //继续Fragment类。
public class Fragment2 extends Fragment { @Override
//在Fragment被加载时调用
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//加载某个布局文件。
return inflater.inflate(R.layout.fragment2, null);
} }
5、创建主界面文件。
<LinearLayout 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=".MainActivity" > <fragment
android:id="@+id/fragment1"
android:name="com.ljh.fragmentdemo.Fragment1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" /> <fragment
android:id="@+id/fragment2"
android:name="com.ljh.fragmentdemo.Fragment2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" /> </LinearLayout>
6、创建主类。
本步骤的关键是用Fragment对象取代布局文件中的内容。
package com.ljh.fragmentdemo; import com.ljh.fragmentdemo.R; import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //获得FragmentManager,然后获取FragmentTransaction。
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
//用Fragment动态代替布局文件中的内容
transaction.replace(R.id.fragment1, new Fragment1());
transaction.replace(R.id.fragment2, new Fragment2());
//提交事务
transaction.commit();
} }
注:
1、对两个Fragment的分别进行编辑,如
@Override
//在Fragment被加载时调用
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//加载某个布局文件。
View root = inflater.inflate(R.layout.fragment1, null); TextView tvContent = (TextView) root.findViewById(R.id.tv_content);
tvContent.setText("hello"); return root;
}
2、其它内容见后面博客:
(1)Android2.3及以前对Fragment的支持。
(2)使用Fragment使不同的尺寸的屏幕展现不同内容。
(3)Activity与Fragment之间的通信。
Fragment之一:Fragment入门的更多相关文章
- fragment基础 fragment生命周期 兼容低版本
fragment入门 ① 创建一个类继承Fragment 重写oncreateView方法 public class FirstFragment extends Fragment { @Overrid ...
- RadioGroup+Fragment 使用Fragment的add()方法,防止使用replace每次都重新加载页面,造成资源浪费
radiogroup+fragment是很常用的主页导航控件,之前为了代码简便一直使用的replace()替换fragment,代码如下: getSupportFragmentManager().be ...
- android fragment嵌套fragment出现的问题:no activity
package com.example.fragmentNavigation2.fragment; import android.content.Context; import android.os. ...
- 关于Fragment与Fragment、Activity通信的四种方式
一直想总结一下Fragment与Fragment.Activity通信的问题,今天有时间一共总结了三种,权当抛砖引玉,如果大家还有更好的方式来实现Fragment和Fragment.Activity的 ...
- activity 嵌套一级fragment,一级fragment嵌套二级fragment,在一级fragment中刷新二级fragment中的UI
今天遇到挺纠结的问题,由于产品设计的问题,技术上涉及到activity 嵌套一级fragment,一级fragment嵌套二级fragment,在一级fragment中刷新二级fragment中的UI ...
- TabActivity 、fragemnt+fragment 和 Fragment+Viewpager 优缺点
1 TabActivity : 1 过时了 . 2 activity . 是作为android的四大组件... 重量级的家伙 ViewGroup : 特别麻 ...
- Fragment(一)--Fragment用法常见问题
fragment notes fragment相关内容包括 基本定义与使用 回退栈内部实现 fragment通信(与activity 与fragment) DialogFragment VP + Fr ...
- Fragment学习(二): 管理Fragment和Fragment通讯
一. 管理Fragment 首先,如果你想在Android3.0及以下版本使用Fragment,你必须引用android-support-v4.jar这个包 然后你写的activity不能再继承自Ac ...
- Fragment 嵌套Fragment注意事项
最近项目新功能需要在垂直方方向可以循环滚动,并且水平方向也可以水平循环滚动,并且可以定位到指定item上.很自然的想到了ViewPager和 VerticalViewPager来解决项目需求,UI的大 ...
- Android 中关于Fragment嵌套Fragment的问题
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/5802146.html 问题描述: 在项目中Activity A中嵌套Fragment B,Fragment ...
随机推荐
- 关于Android Assets读取文件为File对象
关于Android Assets读取文件为File对象的问题,在Assets里面放置文件,在使用的时候,一般是使用AssetManger对象,open方法获取InputStream 然后进行其他操作. ...
- wdcp/wdlinux 在 UBUNTU/linux 中安装失败原因之创建用户
根本原因在于安装时创建的用户www 使用了和ubuntu已创建的用户,冲突了自然创建不了用户. 你可以修改lanmp.sh脚本中创建www用户时的代码,将1000改为其他数字. 也可以修改当前用户的U ...
- Django auth 登陆后页面跳转至/account/profile,修改跳转至其他页面
这几天在学习django,django功能很强大,自带的auth,基本可以满足用户注册登陆登出,简单的用户注册登陆系统使用django auth足矣.当然也不是拿来就能用的,需要自己写登陆页面的模板, ...
- Rectangle and Square
Description Little Petya very much likes rectangles and especially squares. Recently he has received ...
- What is an http upgrade?
HTTP Upgrade is used to indicate a preference or requirement to switch to a different version of HTT ...
- 工作中小知识点汇总(c#)
1.OOP 实体与数据库字段转换(注意 此时实体字段必须和数据库中查询的字段列名相同) list = ModelExtend.GetByDataTablePart<EZRate>(ds.T ...
- MySQL库目录下db.opt文件的作用
细心的朋友可能会发现有时候在某些库目录下有个 db.opt 文件,那这个文件是干什么用的呢?如果你用vi等编辑器打开看的话,内容很简单,是用来记录该库的默认字符集编码和字符集排序规则用的.也就是说如果 ...
- socket基础(二)
Microsoft.Net Framework为应用程序访问Internet提供了分层的.可扩展的以及受管辖的网络服务,其名字空间System.Net和System.Net.Sockets包含丰富的类 ...
- SQL数据转移
方法一:如果想把数据库A中的表Table1中的数据复制到数据库B中的表Table2中,也就是要预先建立Table2,可以使用一下语句: use B goinsert into Table2 selec ...
- Linux系统编程(23)——信号的阻塞
实际执行信号的处理动作称为信号递达(Delivery),信号从产生到递达之间的状态,称为信号未决(Pending).进程可以选择阻塞(Block)某个信号.被阻塞的信号产生时将保持在未决状态,直到进程 ...