2.使用方法跟Activity很類似,但是他不用再到AndroidManifest.xml做 新增Activity的設定。
3.首先,新增一個專案。
4.在main_activity.xml設定畫面。如圖(中間紅框的部分,是指定放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="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginLeft="20dp"
android:layout_marginTop="22dp"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_toRightOf="@+id/button2"
android:text="Button" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button1"
android:layout_marginTop="59dp" >
</FrameLayout>
</RelativeLayout>
5.畫面是拉好了,但是有點醜,稍微修改一下。(如下圖)
<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="${relativePackage}.${activityClass}" >
<LinearLayout
android:id="@+id/layourt1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/Main_Btn_F1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="Fragment1" />
<Button
android:id="@+id/Main_Btn_F2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="Fragment2" />
<Button
android:id="@+id/Main_Btn_F3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="Fragment3" />
</LinearLayout>
<FrameLayout
android:id="@+id/Main_Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layourt1">
</FrameLayout>
</RelativeLayout>
6.在整理後的XML,可以看到Button元件上,有個android:onClick="onClick",這個寫法到時候會對應MainActivity裡面的OnClickListener Function的呼叫(名稱)
7.MainActivity對應的畫面好了,再來不急著先改MainActivity.java,先來製作第一個Fragment的Layout。
8.創建一個新的XML,並配置如下。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="15pt"
android:text="I'm 1 View" />
</RelativeLayout>
7.OK,畫面好了,再來製作Fragment的Java檔,創建一個新的Class,並繼承Fragment。
8.創好之後呢,點進去Fragment1.java,復寫CreateView的程序(在空白處右鍵,選擇Source->Overwhite/inflate->onCreatView打勾->OK。
9.就會出現以下畫面。這邊就是設定Fragment1.java要對應哪個XML的地方了。
10.修改Fragment1.java,如下。
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
private View currentView; //主視窗
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//設定來源 (XML,Parent,AttachToRoot)
currentView = inflater.inflate(R.layout.fragment_1, container,false);
return currentView;
}
}
11.好,再來就是改MainAcitivity.java了。修正如下。(Fragment2、3做法相同,就先略過了。)
package com.doroto.dfragment;
import com.doroto.fragments.Fragment1;
import com.doroto.fragments.Fragment2;
import com.doroto.fragments.Fragment3;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 按鈕處發事件
*/
@Override
public void onClick(View v) {
FragmentTransaction transaction;
switch(v.getId()){
case R.id.Main_Btn_F1:
transaction = getFragmentManager().beginTransaction();//取得Fragment配置
transaction.replace(R.id.Main_Fragment, new Fragment1()); //指定Fragment覆蓋在Main_Fragment的位置
transaction.commitAllowingStateLoss();//連結Fragment
break;
case R.id.Main_Btn_F2:
transaction = getFragmentManager().beginTransaction();//取得Fragment配置
transaction.replace(R.id.Main_Fragment, new Fragment2()); //指定Fragment覆蓋在Main_Fragment的位置
transaction.commitAllowingStateLoss();//連結Fragment
break;
case R.id.Main_Btn_F3:
transaction = getFragmentManager().beginTransaction();//取得Fragment配置
transaction.replace(R.id.Main_Fragment, new Fragment3()); //指定Fragment覆蓋在Main_Fragment的位置
transaction.commitAllowingStateLoss();//連結Fragment
break;
}
}
}
解說:
1.按鈕觸發事件 onClick 這個Function名稱,跟main_activity.xml裡面Button的
android:onClick="onClick"是相關聯的。
2.transaction.replace 是替換,當然他還有transaction.add(新增)。
3.Fragment必須得commit或者commitAllowingStateLoss 之後才會顯示在畫面上,而這兩個方
法差別在於,前者是單純的連接,後者是連接時會加入一個節點,讓使用者按Back時會回
到節點,而不是直接關閉Activity。
12.完成。










學到了,感謝
回覆刪除