When we design the application, we hope to be able to do our best to fit a variety of devices, including 4-inch screen, 7-inch screen, 10-inch screen and so on, Android development documentation for our reference, and Google Io app (ii) also realized this idea, they are using layout, Layout-large inside different layout file implementation, the following is a translation of the developer.android.com an article, the example can be seen in detail layout, layout-large and use fragmen to build a flexible desktop. When designing applications, you can reuse fragment in different layout structures to support numerous screen sizes, optimizing the user experience on available screen space. For example, on a handheld device (such as a Nexus 4), a screen displays a fragment, and multiple fragment can be used to display information on a larger screen, such as a Nexus 7. Such as: Figure A Figure one, in the large screen two fragment display in a screen, but the handheld device, need two screens display, one screen can only show one, they need to guide each other. The Fragmentmanager class provides methods that allow you to add, delete, and replace fragment while the activity is running to create a flexible, dynamic experience. Add fragment to a running activityThis is not like the Android UI Development The 17th--android fragment instance puts the label in the layout file. Instead, use Fragmentmanager dynamic management fragment. Fragmentmanager Create a fragmenttransaction,It provides APIs for adding, deleting, and other fragment transactions. Activity allows removal or replacement of fragment requires the following conditions:1. Add the initialized fragment to the activity's OnCreate () method 2. There must be a view container in the layout of the fragment placement In the program example, the Res/layout/news_articles.xml file provides a view container. [HTML]View Plaincopy
- <framelayout < span class= "attribute" >xmlns:android= "http://schemas.android.com/apk/res/android"
- android:id=< Span class= "Attribute-value" > "@+id/fragment_container" &NBSP;&NBSP;
- android:layout_width= "match_parent"
- android:layout_height= "match_parent" />
The activity uses Getsupportfragmentmanager () to get Fragmentmanager, and then calls BeginTransaction to create a Fragmenttransaction object, and then calls Add ( ) method to add a fragment. In activity, you can use the same Fragmenttransaction object to perform multiple fragment transactions, and you must call the Commint () method when doing so. The following code shows how to add a fragment to Res/layout/news_articles.xml layout:[Java]View Plaincopy
- Import Android.os.Bundle;
- Import android.support.v4.app.FragmentActivity;
- Public class Mainactivity extends Fragmentactivity {
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.news_articles);
- //Check The activity is using the layout version with
- //The Fragment_container framelayout
- if (Findviewbyid (r.id.fragment_container) = null) {
- //However, if we ' re being restored from a previous state,
- /Then we don ' t need to do anything and should return or else
- //We could end up with overlapping fragments.
- if (savedinstancestate! = null) {
- return;
- }
- //Create An instance of Examplefragment
- Headlinesfragment firstfragment = new Headlinesfragment ();
- This activity is started with special instructions from an Intent,
- //Pass the Intent ' s extras to the fragment as arguments
- Firstfragment.setarguments (Getintent (). Getextras ());
- //Add The fragment to the ' Fragment_container ' framelayout
- Getsupportfragmentmanager (). BeginTransaction ()
- . Add (R.id.fragment_container, firstfragment). commit ();
- }
- }
- }
The fragment here is added to the framelayout at run time, instead of directly using the tag definition in the activity's layout, the activity can either remove it or replace it with a different fragment. Replace fragmentThe process of replacing a fragment is similar to adding a fragment, but requires the replace () method instead of the Add () method. It is important to note that when executing a fragment transaction, such as replacing or deleting a fragment, if you want to be able to fall back to the current, you must call the Addtobackstack () method before you commit the fragment transaction. When a transaction is added to the stack when a fragment is removed or replaced, the removed Fragmeng is not extinct and fragment restarts if the user returns. If it is not put into the stack, when fragment is replaced or removed, the fragment dies. Here is an example of replacing fragment: [Java]View Plaincopy
- Create fragment and give it an argument specifying the article it should show
- Articlefragment newfragment = new Articlefragment ();
- Bundle args = new bundle ();
- Args.putint (Articlefragment.arg_position, POSITION);
- Newfragment.setarguments (args);
- Fragmenttransaction transaction = Getsupportfragmentmanager (). BeginTransaction ();
- Replace whatever is in the Fragment_container view with this fragment,
- and add the transaction to the back stack so the user can navigate back
- Transaction.replace (R.id.fragment_container, newfragment);
- Transaction.addtobackstack (null);
- Commit the transaction
- Transaction.commit ();
The Addtobackstack () method has an optional string parameter that specifies a unique name for the transaction, which is a non-essential.
Reference: http://developer.android.com/training/basics/fragments/fragment-ui.html Figure II Google IO APP /*** @author Zhang Xingye * http://blog.csdn.net/xyz_lmn* Android Development Advanced Group: 241395671*/ |