Just like the user control in WinForm, you can add a user control directly to the specified location when a page is initialized, or it can be added dynamically with more data.
For Android fragment, there are two ways, one called static loading, and one called dynamic loading.
Whether it's static or dynamic, there's a fragment activity first.
<Relativelayoutxmlns: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:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Android:paddingbottom= "@dimen/activity_vertical_margin"Tools:context= "Com.example.administrator.myapplication.fragment"><TextViewAndroid:text= "Hello"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/sk"/></Relativelayout>
Packagecom.example.administrator.myapplication;Importandroid.app.Activity;Importandroid.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.TextView; Public classFragmentextendsFragment {//must inherit this class@Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {return Super. Oncreateview (Inflater, container, savedinstancestate); }}
Then build another activity to show the static and dynamic methods of loading fragment
<LinearLayoutxmlns: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:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Android:paddingbottom= "@dimen/activity_vertical_margin"Tools:context= "Com.example.administrator.myapplication.Main2Activity" > <FragmentAndroid:id= "@+id/fragment"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:name= "Com.example.administrator.myapplication.fragment"Android:tag= "Here is the static load, the above parameter name, is the class of fragment"> </Fragment> <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:id= "@+id/frame"android:orientation= "Horizontal"Android:tag= "Here is the dynamic loading, only need to have an ID, backstage according to this ID, in this layout inside load a fragment"> </LinearLayout></LinearLayout>
In the background code of this page, the following shows the static loading and dynamic loading
protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_fragment);//This method must have, although the fill is not the current page, then the loaded page is the current page//The following is a static load, which allows access to the controls inside the fragment when statically loadedTextView textview=(TextView) Findviewbyid (r.id.sk); Textview.settext (Hello); //a detailed step-up of dynamic downloadFragment fragment =NewFragment ();//instantiate this fragment class.Fragmentmanager Fragmentmanager =Getfragmentmanager (); Fragmenttransaction Tran=fragmentmanager.begintransaction ();//Get a transactionTran.add (Fragment,NULL);//Add the contents of this transactionTran.commit ();//Commit a transaction}
Fragment usage (similar to. NET user controls, good reusability) .... ---------------Android