慕课网学习:https://www.imooc.com/video/15817
一:静态使用Fragment
1、根据需求创建Fragment
①、创建子类继承Fragment
②、重写onCreatView()方法,为Fragment设置xml布局文件,转换成view对象返回
2、在activity布局中,通过<fragment>标签引入fragment
android:name="fragment 的包名.类名"
3、源代码:
①、代码布局、实现效果图
②、源代码
TitleFragment.java
package com.fragment;
import com.example.fragment02.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;
/**
* 创建和使用fragment的步骤
* 1、创建子类继承fragment
* 2、重写onCreatView()方法 该方法主要定义fragment的布局 以view对象的形式返回fragment的视图
* 3、将fragment引入到Activity中
* @author x
*
*/
public class TitleFragment extends Fragment {
/**
* 表示fragment第一次创建绘制用户界面时系统回调的方法
* 返回值view表示当前加载fragment视图 如果fragment不提供视图可以返回null
* LayoutInflater inflater,表示布局填充或者加载器 将xml文件转换为view对象
* ViewGroup container,表示当前fragment插入activity的布局视图对象
* Bundle savedInstanceState 表示存储上一个fragment的信息
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//表示将指定资源的xml文件转换成具体的view对象 inflate(表示加载xml文件的资源id,null)
View view=inflater.inflate(R.layout.fragment_title, null);
RelativeLayout layout=(RelativeLayout) view.findViewById(R.id.rl_layout);
layout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getActivity(), "我是标题栏", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
ContentFragment.java
package com.fragment;
import com.example.fragment02.R;
import android.app.Fragment;
import android.os.Bundle;
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) {
return inflater.inflate(R.layout.fragment_content, null);
}
}
fragment_title.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/bar_bg"
android:id="@+id/rl_layout">
<ImageView
android:id="@+id/iv_back"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_centerVertical="true"
android:paddingLeft="15dp"
android:scaleType="centerCrop"
android:src="@drawable/back_unclik"
/>
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/title_content"
android:textColor="#ffffff"
android:textSize="20sp"
/>
</RelativeLayout>
fragment_content.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" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/content"
android:textSize="20sp"
android:textStyle="bold"
/>
</LinearLayout>
MainActivity.java
package com.example.fragment02;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.os.Build;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//去除标题栏
setContentView(R.layout.fragment_main);
}
}
fragment_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">
<fragment
android:id="@+id/fragment_title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:name="com.fragment.TitleFragment"
/>
<fragment
android:id="@+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/fragment_title"
android:name="com.fragment.ContentFragment"
/>
</RelativeLayout>
drawable
back_unclik.png bar_bg.png
二:动态使用Fragment
在以上静态使用Fragment基础上修改:
fragment_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">
<LinearLayout
android:id="@+id/title_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
></LinearLayout>
<LinearLayout
android:id="@+id/content_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/title_layout"
></LinearLayout>
</RelativeLayout>
MainActivity.java
package com.example.fragment03;
import com.example.fragement03.R;
import com.fragment.ContentFragment;
import com.fragment.TitleFragment;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.os.Build;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.fragment_main);
//1、创建Fragment的管理器对象
FragmentManager manager=getFragmentManager();
//2、获取Fragment的事务对象并且开启事务
FragmentTransaction transaction=manager.beginTransaction();
//3、调用事务中相应的动态操作Fragment的方法执行 add(表示fragment动态添加位置的资源id,表示添加的fragment对象)
transaction.add(R.id.title_layout, new TitleFragment());
transaction.add(R.id.content_layout, new ContentFragment());
// transaction.remove(arg0); remove(需要移除的fragment对象)
// transaction.replace(arg0, arg1) replace(表示替换fragment位置的资源id,表示替换fragment对象)
//4、提交事务
transaction.commit();
}
}
三:兼容低版本引用v4包
说明:①、extends FragmentActivity ②、getSupportFragmentManager();