1. Using XML tags
1.1 Defining two important properties
<fragment
android:id= "@+id/fregment_top"
Android:layout_width= "Match_parent"
android:layout_height= "220DP"
android:layout_margintop= "80DP"
android:name= "Com.lzh.fragment_study. Framgment_first "
/>
1.2 Implementing a new class that inherits from fragment
public class Framgment_first extends Fragment {
Public Framgment_first () {
}
@Override
Public View Oncreateview (layoutinflater inflater, ViewGroup container,
Bundle savedinstancestate) {
View Rootview = inflater.inflate (R.layout.fragment_main, container,
FALSE);
return rootview;
}
}
1.3 Create a fragment layout file Fragment_main
<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:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= "Com.lzh.fragment_study. Mainactivity$placeholderfragment ">
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"
/>
</RelativeLayout>
2. Implementation via Java code
Adding fragment to the program
Fragmentmanager Fragmentmanager = Getsupportfragmentmanager ();
Fragmenttransaction fragmenttransaction = Fragmentmanager.begintransaction ();
Examplefragment fragment = new Examplefragment ();
Fragmenttransaction.add (R.id.container, fragment);
Fragmenttransaction.commit ();
Getsupportfragmentmanager (). BeginTransaction ()
. Add (R.id.container,new Framgment_first ()). commit ();
Android Fragment created