Fragment之一:Fragment入门

参考自张泽华视频html


Fragment是自Android3.0后引入的特性,主要用于在不一样的屏幕尺寸中展示不一样的内容。java

Fragment必须被嵌入Activity中使用,老是做为Activity的组成部分。android


简单示例:app

一个Activity的界面由2个部分组成,每一个部分分别是一个Fragment。ide

效果图以下:布局



一、建立第一个Fragment的界面文件。code

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>

二、建立第一个Fragment的类文件。xml

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);
	}
}

三、建立第二个Fragment的界面文件。htm

<?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>

四、建立第二个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);
	}

}

五、建立主界面文件。

<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>

六、建立主类。

本步骤的关键是用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();
	}

}

注:

一、对两个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;
	}

二、其它内容见后面博客:

(1)Android2.3及之前对Fragment的支持。

(2)使用Fragment使不一样的尺寸的屏幕展示不一样内容。

(3)Activity与Fragment之间的通讯。

相关文章
相关标签/搜索