2025-05-17

在之前的教程中,我們已經學習瞭什麼是Activity以及如何使用它。在一個小屏幕的設備上,一個activity通常占據瞭整個屏幕,同時顯示各種UI視圖組件。Activity實際上就是視圖的容器。然後,當一個activity被顯示在一個大屏幕的設備上,例如平板電腦,總會顯得有些不適應。因為屏幕太大瞭,activity中的所有UI組件要充滿整個屏幕,這樣一來,視圖的層次結構就很復雜瞭。一個更好的辦法是使用一種“輕量級”的activity,每個“輕量級”activity包含自己的視圖,互不幹擾。在運行期間,根據屏幕的方向和尺寸,一個activity可以包含一個或多個“輕量級”activity。在Android3.0以上的版本,這種“輕量級”的activity叫做Fragment.

    可以把Fragment想象成Activity的另外一種形式。你創建fragments去包含UI組件,就像創建activities那樣。但是,Fragment總是被嵌在Activity中。

    1.創建一個名為Fragments的工程。

    2.在res/layout文件夾下,新建一個叫fragment1.xml的文件。

[java] <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#00FF00" 
    android:orientation="vertical" > 
 
    <TextView 
        android:id="@+id/lblFragment1" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="This is fragment #1" 
        android:textColor="#000000" 
        android:textSize="25sp" /> 
 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#00FF00"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/lblFragment1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is fragment #1"
        android:textColor="#000000"
        android:textSize="25sp" />

</LinearLayout>    3.在res/layout文件夾下,新建一個叫fragment2.xml的文件。

[java] <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFFE00" 
    android:orientation="vertical" > 
 
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="This is fragment #2" 
        android:textColor="#000000" 
        android:textSize="25sp" /> 
 
    <Button 
        android:id="@+id/btnGetText" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:onClick="onClick" 
        android:text="Get text in Fragment #1" 
        android:textColor="#000000" /> 
 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFE00"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is fragment #2"
        android:textColor="#000000"
        android:textSize="25sp" />

    <Button
        android:id="@+id/btnGetText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Get text in Fragment #1"
        android:textColor="#000000" />

</LinearLayout>    4.main.xml中的代碼。

[java] <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" > 
 
    <fragment 
        android:id="@+id/fragment1" 
        android:name="net.learn2develop.Fragments.Fragment1" 
        android:layout_width="0px" 
        android:layout_height="match_parent" 
        android:layout_weight="1" /> 
 
    <fragment 
        android:id="@+id/fragment2" 
        android:name="net.learn2develop.Fragments.Fragment2" 
        android:layout_width="0px" 
        android:layout_height="match_parent" 
        android:layout_weight="1" /> 
 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/fragment1"
        android:name="net.learn2develop.Fragments.Fragment1"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/fragment2"
        android:name="net.learn2develop.Fragments.Fragment2"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>    5.新建兩個類:Fragment1.java和Fragment2.java。

    6.Fragment1.java中的代碼。

[java] package net.learn2develop.Fragments; 
 
import android.app.Activity; 
import android.app.Fragment; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
 
public class Fragment1 extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) { 
 
        Log.d("Fragment 1", "onCreateView"); 
 
        // —Inflate the layout for this fragment—  
        return inflater.inflate(R.layout.fragment1, container, false); 
    } 

package net.learn2develop.Fragments;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        Log.d("Fragment 1", "onCreateView");

        // —Inflate the layout for this fragment—
        return inflater.inflate(R.layout.fragment1, container, false);
    }
}
    7.Fragment2.java中的代碼。

[java] package net.learn2develop.Fragments; 
 
import android.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 
 
public class Fragment2 extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) { 
        // —Inflate the layout for this fragment—  
        return inflater.inflate(R.layout.fragment2, container, false); 
    } 

package net.learn2develop.Fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Fragment2 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // —Inflate the layout for this fragment—
        return inflater.inflate(R.layout.fragment2, container, false);
    }
}
    8. 調試。

 

效果圖:

  

摘自  horsttnann的專欄
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *