Fragment was introduced by android3.0, why did Google launch fragment? The main purpose is to support a more dynamic and flexible UI design on large-screen devices, such as tablets. Tablet screens are much larger than phones, have more room to put more UI components, and have more interaction between these components, andfragment allows such a design without requiring you to manage the Viewhierarchy complex changes yourself. By dispersing the activity's layout into fragment, you can modify the appearance of the activity at run time, and you can think of fragment as part of the activity interface, first of all:
The first picture we see, click on the left item to jump to the right of the layout display, this time to start an activity, and the following figure click on the left side of the item, can be displayed on the right, with fragment to display on the line, instead of activating activity, We know that activity is a component of Android, so it consumes more memory than fragment, which is why it is recommended to use fragment on a larger screen.
Now create an Android project Fragment1
Activity_main.xml
<linearlayout 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:orientation= "Horizontal" tools:context= ". Mainactivity "> <fragment android:name=" com.example.fragment1.Fragment1 " android:id=" @+id/ Fragment1 " android:layout_width=" 0dip " android:layout_height=" match_parent " android:layout_weight = "1"/> <fragment android:name= "Com.example.fragment1.Fragment2" android:id= "@+id/ Fragment2 " android:layout_width=" 0dip " android:layout_height=" match_parent " android:layout_ weight= "1"/></linearlayout>
Found a node in the layout fragment, and our previous layout view is the beginning of capital letters, such as: TextView, so fragment is not a View object, but a type, Android: Name refers to the full class name of the fragment class, so Fragment1 to inherit the fragment object, if it is under the Android.app.Fragment package, then the minimum version specified must be one (android: minsdkversion= "11") less than 11 program will be error, because the system fragment is 3.0 appears,
Fragment1.java
Package Com.example.fragment1;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 {@Overridepublic View oncreateview (layoutinflater inflater, ViewGroup container,bundle savedinstancestate) { Return inflater.inflate (r.layout.fragment1, NULL);}}
Fragment1.xml
<pre name= "code" class= "java" ><?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= " Http://schemas.android.com/apk/res/android " android:layout_width=" match_parent " android:background=" # ff0000 " android:layout_height=" match_parent " android:gravity=" center " android:orientation=" Vertical "> <textview android:id=" @+id/textview1 " android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:text= "I am the first fragment" android:textappearance= "? Android: Attr/textappearancelarge "/></linearlayout>
Fragment2.java
Package Com.example.fragment1;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;public class Fragment2 extends Fragment {@Overridepublic View oncreateview (layoutinflater inflater, ViewGroup container,bundle savedinstancestate) { Return inflater.inflate (R.layout.fragment2, NULL);}}
Fragment2.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:background=" #0000ff " android:layout_height=" Match_parent " android:gravity=" center " android:orientation=" vertical "> <textview android : id= "@+id/textview1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content " android:text= "I am the second fragment" android:textappearance= "Android:attr/textappearancelarge"/></ Linearlayout>
Mainactivity.java
Package Com.example.fragment1;import Android.os.bundle;import Android.app.activity;public class MainActivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);}}
We see that the Mainactivity class does not write any code, which is statically created fragment:
Android fragment Getting Started