normally fragment as part of the host activity UI is embedded as part of the activity's entire view hierarchy. There are 2 ways you can add a fragment to activity layout:
First, declare fragment in the activity's layout file.You can specify the Layout property (sdk3.0 later) for fragment, as you would for a view.The sample is an activity with 2 fragment:
<?xml version="1.0" encoding="Utf-8"?>
<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"
android:orientation="Horizontal "
android:layout_width="match_parent "
android:layout_height="match_parent">
<fragment android:name= " Com.example.news.ArticleListFragment "
android:id="@+id/list"
android:layout_weight="1"
< c23> android:layout_width="0DP"
android:layout_height="match_parent" />
<fragment android:name= " Com.example.news.ArticleReaderFragment "
android:id="@+id/viewer"
< c23> android:layout_weight="2"
android:layout_width="0DP"
< c23> android:layout_height="match_parent" />
</LinearLayout>
The Android:name property in <fragment> specifies the fragment class that is instantiated in layout.
When the system creates this activity layout, it instantiates each of the fragment specified in layout and invokes the Oncreateview () method on each to get the layout of each fragment. The system will insert the View returned from fragment directly into the location of the <fragment> element.
Note : Each fragment requires a unique identity, assuming the activity restarts, the system can be used to recover the fragment (and you can also use it to capture fragment to handle transactions, such as removing it.)
There are 3 ways to provide an identity for a fragment:
- Provides a unique ID for the Android:id property.
- Provides a unique string for the Android:tag property.
- Assuming that none of the above 2 you have provided, the system uses the ID of the container view.
Second, use Fragmentmanager to add fragment to an existing viewgroup.
fragment can be added to the activity whenever the activity is executed Layout. Simply specify a viewgroup that needs to be placed fragment. To manipulate fragment transactions in your activity (such as joining, removing, or replacing a fragment), you must use the The Fragmenttransaction API.
You can obtain an instance of fragmenttransaction from your activity by, for example, the following methods:
Fragmentmanager Fragmentmanager = Getfragmentmanager ();
Fragmenttransaction fragmenttransaction = Fragmentmanager.begintransaction ();
you can then use the Add () method to add a fragment, specify the fragment to join, and the view to insert.
Examplefragment fragment = new examplefragment ();
F Ragmenttransaction.add (R.id.fragment_container, fragment);
Fragmenttransaction.commit ();
the first parameter of add () is the viewgroup to be placed by the fragment, specified by the resource ID, and the second is the fragment to be added. Once the change is made with fragmenttransaction, in order for the change to take effect, Commit () must be called. /**
* @author Zhang Xingye
* Email: xy-zhang#163.com
* Android Development Advanced Group:278401545
*
*/
Android Fragment Use