Android TabActivity usage
TabActivity first has a TabActivity in Android for our convenience. The following functions are available:Public TabHost getTabHost ()Obtains the TabHost of the current TabActivity.Public TabWidget getTabWidget ()Obtains the TabWidget of the current TabActivity.Public void setDefaultTab (String tag)These two functions are easy to understand, that is, to set the default TabPublic void setDefaultTab (int index)Use the tab name -- tag or index (starting from 0)Protected void onRestoreInstanceState (Bundle state)These two functions can be introduced.Protected void onSaveInstanceState (Bundle outState)Refer to the Activity lifecycle TabHost. The Tab carrier we need to use is TabHost, which needs to be obtained from TabActivity. getTabHost. Now let's look at the TabHost class. It has three embedded classes: one class TabHost. TabSpec, two interfaces TabHost. TabContentFactory and TabHost. OnTabChangeListener. These classes and interfaces will be introduced later. Some functions of the TabHost class:Public void addTab (TabHost. TabSpec tabSpec)Add a tab. The TabHost. TabSpec parameter is returned using the following function.Public TabHost. TabSpec newTabSpec (String tag)Create TabHost. TabSpecPublic void clearAllTabs ()Remove all TabsPublic int getCurrentTab () Public String getCurrentTabTag () Public View getCurrentTabView () Public View getCurrentView () Public FrameLayout getTabContentView ()Returns FrameLayout of the Tab content.Public TabWidget getTabWidget () Public void setCurrentTab (int index)Set the current Tab by indexPublic void setCurrentTabByTag (String tag)Set the current Tab by tagPublic void setOnTabChangedListener (TabHost. OnTabChangeListener l)Set Response Processing for TabChanged eventsPublic void setup ()This function introduces TabHost. TabSpec. You can see how to add a tab from the above function. Note that the Tag here is not the text on the Tab button. To set the label and content of a tab, you must set the TabHost. TabSpec class. Reference the words in the SDK -- "A tab has a tab indicator, content, and a tag that is used to keep track of it.", TabHost. TabSpec is to manage these three things:Public String getTag () Public TabHost. TabSpec setContent Public TabHost. TabSpec setIndicatorI understand whatIndicatorIs the label on the Tab, it canSet label:SetIndicator (CharSequence label)OrSet label and icon:SetIndicator (CharSequence label, Drawable icon)Or directlySpecify a view:SetIndicator (View view)ForContentIs the content in the Tab, you canSet the View id:SetContent (int viewId)OrTabHost. TabContentFactoryTo process:SetContent (TabHost. TabContentFactory contentFactory)Or useNew IntentTo introduce the content of other activities:SetContent (Intent intent)
Main program code
Code in Acitvit
- Package com. yang. tabletest;
-
- Import android. app. TabActivity;
- Import android. OS. Bundle;
- Import android. view. LayoutInflater;
- Import android. widget. TabHost;
-
- Public class tabletestaciti.pdf extends TabActivity {
- /** Called when the activity is first created .*/
- @ Override
- Public void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- // SetContentView (R. layout. main );
-
- // Obtain the TabHost of the current TabActivity
- TabHost tabHost = getTabHost ();
-
- LayoutInflater. from (this). inflate (R. layout. tabs1, tabHost. getTabContentView (), true );
-
- TabHost. addTab (tabHost. newTabSpec ("tab1 ")
- . SetIndicator ("Homepage ")
- . SetContent (R. id. view1 ));
-
-
- TabHost. addTab (tabHost. newTabSpec ("tab2 ")
- . SetIndicator ("title ")
- . SetContent (R. id. view2 ));
- TabHost. addTab (tabHost. newTabSpec ("tab3 ")
- . SetIndicator ("Introduction ")
- . SetContent (R. id. view3 ));
- TabHost. addTab (tabHost. newTabSpec ("tab4 ")
- . SetIndicator ("about ")
- . SetContent (R. id. view4 ));
- }
-
-
- }
Code in tabls. xml: Tabi. xml Code
- <FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android" Android: layout_height = "fill_parent">
-
- Android: background = "@ drawable/blue"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- Android: text = "@ string/tabs_1_tab_1"/>
-
- Android: background = "@ drawable/red"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- Android: text = "@ string/tabs_1_tab_2"/>
-
- Android: background = "@ drawable/green"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- Android: text = "@ string/tabs_1_tab_3"/>
-
- Android: background = "@ drawable/green"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- Android: text = "@ string/tabs_rjtab_4"/>
-
- </FrameLayout> string. xml Code
Java code
-
-
- Hello World, tabletestaciti.pdf!
- AFU Learning
- Home Page
- Title
- About
- Return
- Color. xml Code Java code
-
-
- # 404040ff
-
- # Ff00ff
- # 0ff0ff
- # C0c0c0ff
-
- # FfFF33ff
- #00 ffff
- # 808080ff
- # Ff6699ff
- #66 ffffff
- #000000
- # FFFFFF
- The Activity code Java code in the second example
- Package com. yang. tabletest;
-
- Import android. app. TabActivity;
- Import android. OS. Bundle;
- Import android. view. View;
- Import android. widget. TabHost;
- Import android. widget. TextView;
-
- Public class TableTestAcitivity extends TabActivity implements TabHost. TabContentFactory {
- /** Called when the activity is first created .*/
- @ Override
- Public void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
-
- Final TabHost tabHost = getTabHost ();
- TabHost. addTab (tabHost. newTabSpec ("tab1 ")
- . SetIndicator ("Homepage", getResources (). getDrawable (R. drawable. test ))
- . SetContent (this ));
- TabHost. addTab (tabHost. newTabSpec ("tab2 ")
- . SetIndicator ("title", getResources (). getDrawable (R. drawable. test ))
- . SetContent (this ));
- TabHost. addTab (tabHost. newTabSpec ("tab3 ")
- . SetIndicator ("about", getResources (). getDrawable (R. drawable. test ))
- . SetContent (this ));
- }
-
- @ Override
- Public View createTabContent (String arg0 ){
- Final TextView TV = new TextView (this );
- TV. setText ("Content for tab with tag" + arg0 );
-
- Return TV;
- }
-
-
-
-
- }
- The size is 14.3 KB.
- TableTest.zip (25.4 KB)
- Downloads: 563