Fragment建立

程式碼修行者發表於2015-07-22

1、靜態載入

1、fragment layout
2、fragment類
3、在對應的activity layout中載入 fragment

1、fragment layout

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/radioGroup"
        android:orientation="horizontal">

        <RadioButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@mipmap/ic_launcher"
            android:text="靜態載入"
            android:button="@null"
            android:gravity="center_horizontal"
            android:background="@drawable/radio_pressed"
            android:id="@+id/btn_static"
            />

        <RadioButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@mipmap/ic_launcher"
            android:text="動態載入"
            android:button="@null"
            android:gravity="center_horizontal"
            android:background="@drawable/radio_pressed"
            android:id="@+id/btn_dynamic"
            />

        <RadioButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@mipmap/ic_launcher"
            android:text="生命週期"
            android:button="@null"
            android:gravity="center_horizontal"
            android:background="@drawable/radio_pressed"
            android:id="@+id/btn_life"
            />
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@mipmap/ic_launcher"
            android:text="傳遞資料"
            android:button="@null"
            android:gravity="center_horizontal"
            android:background="@drawable/radio_pressed"
            android:id="@+id/btn_data"
            />
    </RadioGroup>
</LinearLayout>

2、fragment class

package com.example.administrator.fragment2;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.zip.Inflater;

/**
 * Created by Administrator on 2015/7/22.
 */
public class Fragment extends android.app.Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        /**
         * 將 layout轉成 view物件
         */
        View view = inflater.inflate(R.layout.fragment,container,false);
        return view;
    }
}

3.在對應的activity layout中載入 fragment

<RelativeLayout 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" tools:context=".MainActivity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:id="@+id/fragment"
        android:name="com.example.administrator.fragment2.Fragment"/>

</RelativeLayout>

2動態載入

前兩步和靜態載入一樣

不同的地方在於

package com.example.administrator.fragment2;

import android.app.*;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //動態載入 fragment
        Fragment fragment = new Fragment(); //這個是我們自己定義的 Fragment
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        fragmentTransaction.addToBackStack(null); //後退按鍵 允許
        fragmentTransaction.add(R.id.linear,fragment);
        fragmentTransaction.commit();
    }

}

這裡寫圖片描述

相關文章