上一个章节,我们说到Android Fragment 支持动态和静态两种使用方式,这个章节我们先来说说Fragment的静态使用方式。Android Fragment 能够定义自己的布局文件,通过一个继承自Fragment的类在重写的OnCreateView方法中加载布局文件,一个Activity会包含若干个Fragment,在Activity的布局文件中将二者的关系关联起来。在Activity加载布局的时候,会去加载Fragment的布局,fragment能够处理自己的事件输入和相应。
1.AndroidTitleFragment
第一个Fragment,继承自Fragment类。能够响应ImageButton的点击事件,弹出一个Toast。
package com.justin.example;
import com.example.androidexample.R;
import android.annotation.SuppressLint;
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.ImageButton;
import android.widget.Toast;
@SuppressLint("NewApi")
public class AndroidTitleFragment extends Fragment {
private ImageButton imageButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_title, container,false);
imageButton = (ImageButton)view.findViewById(R.id.title_btn);
imageButton.setOnClickListener(listener);
return view;
//return super.onCreateView(inflater, container, savedInstanceState);
}
private OnClickListener listener = new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),"I am an ImageButton in TitleFragment ! ",Toast.LENGTH_SHORT).show();
}
};
}
2.fragment_title.xml
AndroidTitleFragment的布局文件。有一个ImageButton和TextView
<?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" >
<ImageButton
android:id="@+id/title_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@drawable/baidu"
android:contentDescription="ImageButton" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am title." />
</LinearLayout>
3.AndroidContentFragment
第二个Fragment,只有一个TextView
package com.justin.example;
import com.example.androidexample.R;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("NewApi")
public class AndroidContentFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_content, container,false);
}
}
4.fragment_content.xml
第二个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:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="I am content."
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
5.AndroidFragmentActivity
package com.justin.example;
import com.example.androidexample.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class AndroidFragmentActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_fragment);
}
}
6.activity_fragment.xml
AndroidFragmentActivity布局文件
<?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" >
<fragment
android:name="com.justin.example.AndroidTitleFragment"
android:id="@+id/title"
android:layout_height="45dp"
android:layout_width="match_parent"/>
<fragment
android:name="com.justin.example.AndroidContentFragment"
android:id="@+id/content"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</LinearLayout>
静态Fragment的使用方法如上所述,下次介绍Fragment的动态使用,着重介绍Fragment的生命周期及与Activity的生命周期的关系。