interface layout, linear layout, horizontal arrangement, average center distribution
Four TextView, Width 0dip, weight is 1, Content Center display
The Body Content section uses the linearlayout control, setting the ID
Create a new four Fragment option class that inherits the Fragment class of the system
Override the Oncreateview () method to return a View Object
Create a new four layout file to display the contents of the tab
In the activity
Gets the linearlayout object, via the Findviewbyid () method
Find four TextView control Objects first
Call the Setonclicklistener () method of the four TextView object , set the Click event, Parameter: this
The current activity implements the Onclicklisntenr interface, implements the OnClick () method, and passes in the View Object
Set the default interface
Gets the fragmentmanager object, via the Getfragmentmanager () method
Call the BeginTransaction () method of the fragmentmanager object to open the transaction and obtain Fragmenttransaction Object
Call the replace () method of the fragmenttransaction object, replacing the current interface with Fragment , Parameters: Containerviewid , the first one Fragment Object
Containerviewid is the IDof the container, which is the ID of thecontent body we define
Invoke the commit () method of the fragmenttransaction object to commit the transaction
Inside the OnClick () method
Call the getId () method of the View object to get the ID of the control that is currently clicked
Switch to determine the ID, according to different IDs to show the different fragment
Fragmenttransaction object's replace () method, Parameter 1 must be a container object, I use TextView alinearlayout control must have a child control under the type conversion exception
There is currently no animation effect
PackageCom.tsh.mytabfrag;Importandroid.app.Activity;Importandroid.app.Fragment;ImportAndroid.app.FragmentManager;Importandroid.app.FragmentTransaction;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.TextView; Public classMainactivityextendsActivityImplementsOnclicklistener {PrivateTextView TV1, TV2, TV3; PrivateFragmentmanager FM; Privatefragmenttransaction ft; PrivateFragment fm1, FM2, FM3; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //Initializing the controlTV1 =(TextView) Findviewbyid (R.ID.TV1); TV2=(TextView) Findviewbyid (R.ID.TV2); TV3=(TextView) Findviewbyid (R.ID.TV3); FM1=NewFragment1 (); FM2=NewFragment2 (); FM3=NewFragment3 (); Tv1.setonclicklistener ( This); Tv2.setonclicklistener ( This); Tv3.setonclicklistener ( This); FM=Getfragmentmanager (); FT=fm.begintransaction (); Ft.replace (R.id.content, FM1); Ft.commit (); } @Override Public voidOnClick (View v) {ft=fm.begintransaction (); Switch(V.getid ()) { Caser.id.tv1:ft.replace (r.id.content, FM1); Break; Caser.id.tv2:ft.replace (r.id.content, FM2); Break; Caser.id.tv3:ft.replace (r.id.content, FM3); Break; default: Break; } ft.commit (); }}
[Android] Create a tab with fragment