Fragment是activity的界面中的一部分或一种行为。你可以把多个Fragment们组合到一个activity中来创建一个多面界面并且你可以在多个activity中重用一个Fragment。你可以把Fragment认为模块化的一段activity,它具有自己的生命周期,接收它自己的事件,并可以在activity运行时被添加或删除。
Fragment分为静态加载和动态加载两种,这里就先讲静态加载,这种方式简单但是不灵活。
一 布局文件
activity_main.xml
<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:orientation="vertical"
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.fragmentdemo.MainActivity" >
<!-- 静态加载fragment:把fragment当成一个ui标签使用 -->
<fragment android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/first"
android:name="com.example.fragmentdemo.FirstFragment"
android:layout_weight="1"/>
<fragment android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.fragmentdemo.SecondFragment"
android:id="@+id/second"
android:layout_weight="1"/>
</LinearLayout>
fragment_first.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="match_parent"
android:background="#00ff00">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="我是第一个Fragment"
android:textColor="#fff"/>
</RelativeLayout>
fragment_second.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="match_parent"
android:background="#ff00ff" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是第第二个Fragment"
android:textColor="#fff"/>
</RelativeLayout>
二 java 文件
FirstFragment.java
package com.example.fragmentdemo;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class FirstFragment extends Fragment{
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
//View.inflate(context, resource, root)
View view = inflater.inflate(R.layout.fragment_first, container,false);
return view;
}
}
SecondFragment.java
package com.example.fragmentdemo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SecondFragment extends Fragment{
@Override
@Nullable
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
//View.inflate(context, resource, root)
View view = inflater.inflate(R.layout.fragment_second, container,false);
return view;
}
}
MianActivity.java
package com.example.fragmentdemo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
三 截图:
四 总结
静态加载Fragment时只需要在布局文件中加在fragment标签中即可,它更像是被当作一个ui标签使用,这种使用简单,但是使用场景单一,极其不灵活,也违背了设计Fragment的初衷。同时在引入Fragment可以引入android.support.v4.app.Fragment也可以引入android.app.Fragment,但是我建议使用android.support.v4.app.Fragment。