Fragment是一種Android 3.0後引入的API ,它出現的初衷是爲了適應平板電腦的大屏幕,手機界面小巧可放下的內容有限,而平板寬闊能夠放下手機上的兩三個界面一塊兒合起來展現,多個Fragment能夠組裝拼接嵌套進一個Activity當中,它的生命週期也會受到Activity宿主的生命週期的影響。Fragment有靜態建立和動態建立兩種方式,靜態建立方式是先建立好Fragment,經過繼承Fragment類,實現onCreateView()方法返回一個頁面視圖View,而後在Activity頁面中嵌套進去,經過Activity的佈局文件XML文件中用標籤中的name屬性引入事先建立好的Fragement的java文件。而動態建立方式的不一樣點在於嵌套進Activity這裏,不是經過XML裏面嵌套進去,是在Activity的java文件裏面經過Fragment的事務對象執行增刪改查替換操做嵌套進去。 下面是一個軟件主界面的製做,使用了4個Fragment嵌套進主頁當中展現。 先上效果圖: java
第一個Fragment製做文件:HomeActivity.javaandroid
import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import main.com.com.R; public class HomeActivity extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.activity_home,null); return view; } }
第二個文件:activity_home.xmlapp
<?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:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="第一個頁面" android:textSize="40sp" android:id="@+id/textView" android:layout_centerInParent="true" /> </RelativeLayout>
第二個Fragment的製做:SearchActivity.javaide
import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import main.com.com.R; public class SearchActivity extends Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.activity_search,null); return view; } }
佈局文件activity_search.xml佈局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="第二個頁面" android:textSize="40sp" android:id="@+id/textView2" android:layout_centerInParent="true" /> </RelativeLayout>
剩下的2個Fragment的製做同樣的就不重複了。 Activity的文件: MainActivity.javacode
import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.RadioGroup; import main.com.com.R; public class MainActivity extends FragmentActivity{ RadioGroup titleGroup; RadioButton radioHome,radioSearch,radioMore,radioPersonal; LinearLayout contentLayout; FragmentManager manager; FragmentTransaction transaction; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); titleGroup=(RadioGroup)findViewById(R.id.titleGroup); radioHome=(RadioButton)findViewById(R.id.radioHome); contentLayout=(LinearLayout)findViewById(R.id.contentLayout); //1.獲取Fragment管理器對象,這裏用的是support.v4.app包下的Fragment,有個app包下Fragment的不同 manager=getSupportFragmentManager(); //2.獲取Fragment事務對象,並開啓事務 transaction=manager.beginTransaction(); //3.調用事務的動態方法這裏是給contentLayout控件下添加一個Fragment頁面 transaction.add(R.id.contentLayout,new HomeActivity()); //4.把添加事務提交,不提交不生效 transaction.commit();//提交後事務對象就沒了,須要還要從新得到 titleGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { transaction=manager.beginTransaction(); switch (checkedId){ case R.id.radioHome: //事務的替換方法,在這個控件下用new HomeActivity()替換舊的Fragment transaction.replace(R.id.contentLayout, new HomeActivity()); break; case R.id.radioSearch: transaction.replace(R.id.contentLayout,new SearchActivity()); break; case R.id.radioMore: transaction.replace(R.id.contentLayout, new MoreActivity()); break; case R.id.radioPersonal: transaction.replace(R.id.contentLayout, new PersonalActivity()); break; } transaction.commit(); } }); } }
Activity的佈局文件:activity_main.javaxml
<?xml version="1.0" encoding="utf-8"?> <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" android:focusable="false" > <LinearLayout android:id="@+id/contentLayout" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:focusable="false" android:background="#ffffff" android:gravity="center" > /> </LinearLayout> <RadioGroup android:id="@+id/titleGroup" android:layout_width="match_parent" android:layout_height="80dp" android:orientation="horizontal" android:layout_alignParentBottom="true" android:layout_below="@+id/line" android:focusable="false" > <RadioButton android:id="@+id/radioHome" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:text="@string/title_home" android:checked="true" android:enabled="true" android:focusable="true" android:button="@null" android:drawableTop="@drawable/home_selected" android:drawablePadding="5dp" android:padding="5dp" /> <RadioButton android:id="@+id/radioSearch" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center" android:layout_gravity="center" android:text="@string/title_search" android:checked="false" android:button="@null" android:drawableTop="@drawable/find_selected" android:drawablePadding="5dp" android:padding="5dp"/> <RadioButton android:id="@+id/radioMore" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:text="@string/title_more" android:checked="false" android:button="@null" android:drawableTop="@drawable/more_selected" android:drawablePadding="5dp" android:padding="5dp"/> <RadioButton android:id="@+id/radioPersonal" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center" android:layout_gravity="center" android:text="@string/title_personal" android:checked="false" android:button="@null" android:drawableTop="@drawable/personal_user" android:drawablePadding="5dp" android:padding="5dp"/> </RadioGroup> </RelativeLayout>