TabWidget is similar to the interface for viewing the phone book in Android. Different content is displayed through multiple tabs. To achieve this, you must first understand TabHost, which is a container for storing multiple Tab labels. Each Tab can correspond to its own layout. For example, the Tab layout in the phone book is a linear layout of a List.
To use TabHost, you must first use the getTabHost method to obtain the TabHost object, and then use the addTab method to add a Tab to the TabHost. Of course, each Tab generates an event during switchover. to capture this event, you need to set the TabActivity event listener setOnTabChangedListener.
1. layout File
<TabHost xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ android: id/tabhost"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<TabWidget
Android: id = "@ android: id/tabs"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
<FrameLayout
Android: id = "@ android: id/tabcontent"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<TextView
Android: id = "@ + id/textview1"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: text = "Linux"
Android: textColor = "# FF0000"/>
<TextView
Android: id = "@ + id/textview2"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: text = "MAC"
Android: textColor = "# javase0f"/>
<TextView
Android: id = "@ + id/textview3"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: text = "Windows"
Android: textColor = "# 1E90FF"/>
</FrameLayout>
</LinearLayout>
</TabHost>
2. Modify MainActivity. Note that it inherits from TabActivity.
Public class MainActivity extends TabActivity {private TabHost tabHost; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); tabHost = getTabHost (); addTab (); // Add a label // set the TabHost background color to tabHost. setBackgroundColor (Color. argb (150, 20, 80,150); // sets the TabHost background image resource tabHost. setBackgroundResource (R. drawable. ic_launcher); // set the tag currently displayed. My understanding is that when you start the program for the first time, the tag is displayed by default. Here the ID of the specified tab starts tabHost from 0. setCurrentTab (0); // tag switching event processing. setOnTabChangedListener indicates that the tag switching event is not a click event, but an event tabHost triggered by switching from one tag to another. setOnTabChangedListener (new OnTabChangeListener () {@ Override public void onTabChanged (String tabId) {alertdiener. builder builder = new AlertDialog. builder (MainActivity. this); Dialog dia; builder. setTitle ("prompt"); builder. setMessage ("currently selected" + tabId + "tag"); builder. setPositiveButton ("OK", new OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {dialog. cancel () ;}}); dia = builder. create (); dia. show () ;}}) ;}// Add a new newTabSped (new TabSpec) label and Icon (setIndicator) and setContent for TabHost) // TabSpec is the newTabSpec () method of the TabHost internal class TabHost object. Return a TabSpec object // public TabSpec newTabSpec (String tag) written in the source code) // {return new TabSpec (tag);} private void addTab () {tabHost. addTab (tabHost. newTabSpec ("tab1 "). setIndicator ("TAB1", getResources (). getDrawable (R. drawable. ic_launcher) // setIndicator () This method is used to set labels and charts. setContent (R. id. textview1); // specify the content as a TextView ---> public TabHost. tabSpec setContent (int viewId) This method requires a viewId as the parameter tabHost. addTab (tabHost. newTabSpec ("tab2 "). setIndicator ("TAB2", getResources (). getDrawable (R. drawable. ic_launcher )). setContent (R. id. textview2); tabHost. addTab (tabHost. newTabSpec ("tab3 "). setIndicator ("TAB3", getResources (). getDrawable (R. drawable. ic_launcher )). setContent (R. id. textview3 ));}}
3. Run the program as follows!