Android Fragment 剖析 - 02

       上一个章节,我们说到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的生命周期的关系。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值