There are two ways to implement the tab View in android. One is to define the <tabhost> label in the layout page, and the other is to inherit tabactivity. however, I prefer the second method. It should be because if the page is complex, your XML file will be written a lot, and the second method is more concise.
Use of the Tab
First, design the page layout.
After the paging design is complete, use the code to create
Tab
Tab, and add
Logo and title
Finally, determine the page layout displayed on each page.Create a "TabDemo" program, which contains three XML files: tab1.xml, tab2.xml, and tab3.xml. The three files use main in the linear layout, relative layout, and absolute layout examples respectively. and define the layout ID as layout01, layout02, and layout03tab1. xml file code
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout android: id = "@ + id/layout01 "...... ...... </LinearLayout> tab2.xml File Code <? Xml version = "1.0" encoding = "UTF-8"?> <AbsoluteLayout android: id = "@ + id/layout02 "...... ...... </AbsoluteLayout> tab3.xml File Code <? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout android: id = "@ + id/layout03 "...... ...... </RelativeLayout>
Import android. app. tabActivity; import android. OS. bundle; import android. widget. tabHost; import android. view. layoutInflater; public class TabDemo extends TabActivity {@ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); TabHost tabHost = getTabHost (); LayoutInflater. from (this ). inflate (R. layout. tab1, tabHost. getTabContentView (), true); // associate the layout file with the TabHost LayoutInflater. from (this ). inflate (R. layout. tab2, tabHost. getTabContentView (), true); LayoutInflater. from (this ). inflate (R. layout. tab3, tabHost. getTabContentView (), true); tabHost. addTab (tabHost. newTabSpec ("TAB1 "). setIndicator ("linear layout "). setContent (R. id. layout01); // setContent () specifies the View tabHost contained in each Tab. addTab (tabHost. newTabSpec ("TAB2 "). setIndicator ("absolute layout "). setContent (R. id. layout02); tabHost. addTab (tabHost. newTabSpec ("TAB3 "). setIndicator ("relative layout "). setContent (R. id. layout03 ));}}
The
8
Statement of line code
TabDemo
Class inheritance and
TabActivity
, Inherited from the past
Activity
Different,
TabActivity
Supports embedded multiple
Activity
Or
View
The
12
The line code passes
GetTabHost ()
Function obtained
Tab
Tab container used
Host clickable
Tab
Tab and page layout.
The
13
The line code passes
LayoutInflater
Set
Tab1.xml
File layout
Tab
Tabs that can be used
View
Object
The
16
Use line code
AddTab ()
The function adds
1
Pages,
TabHost. newTabSpec ("TAB1 ")
Indicates that
12
The
TabHost
, Add an ID
TAB1
Of
Tab
Paging
The
17
Use line code
SetIndicator ()
The function sets the title displayed by page. Use
SetContent ()
Function sets the page layout associated with the page
The second method does not inherit TabActivity. You can define TabHost in the layout file, but the id of TabWidget must be @ android: id/tabs. the id of FrameLayout must be @ android: id/tabcontent.
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
- Android: id = "@ + id/hometabs"
- Android: orientation = "vertical"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent">
- <TabHost android: id = "@ + id/tabhost" // inherits from FrameLayout. Only one node can be displayed, and TabWidget and FrameLayout can be placed under LinearLayout.
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content">
- <LinearLayout
- Android: orientation = "vertical"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent">
- <TabWidget android: id = "@ android: id/tabs"
- Android: orientation = "horizontal"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content">
- </TabWidget>
- <FrameLayout android: id = "@ android: id/tabcontent"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content">
-
- <LinearLayout android: id = "@ + id/ll01" android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent" android: gravity = "center_horizontal"
- Android: orientation = "vertical">
- <EditText android: id = "@ + id/widget34" android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content" android: text = "EditText"
- Android: textSize = "18sp">
- </EditText>
- <Button android: id = "@ + id/widget30" android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content" android: text = "Button">
- </Button>
- </LinearLayout>
- <LinearLayout android: id = "@ + id/ll02" android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent" android: gravity = "center_horizontal"
- Android: orientation = "vertical">
- <AnalogClock android: id = "@ + id/widget36"
- Android: layout_width = "wrap_content" android: layout_height = "wrap_content">
- </AnalogClock>
- </LinearLayout>
- <LinearLayout android: id = "@ + id/ll03" android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent" android: gravity = "center_horizontal"
- Android: orientation = "vertical">
- <RadioGroup android: id = "@ + id/widget43"
- Android: layout_width = "166px" android: layout_height = "98px"
- Android: orientation = "vertical">
- <RadioButton android: id = "@ + id/widget44"
- Android: layout_width = "wrap_content" android: layout_height = "wrap_content"
- Android: text = "RadioButton">
- </RadioButton>
- <RadioButton android: id = "@ + id/widget45"
- Android: layout_width = "wrap_content" android: layout_height = "wrap_content"
- Android: text = "RadioButton">
- </RadioButton>
- </RadioGroup>
- </LinearLayout>
-
- </FrameLayout>
- </LinearLayout>
- </TabHost>
- </LinearLayout>
- TabHost tabHost = (TabHost) findViewById (R. id. tabhost );
- TabHost. setup (); // complete Initialization
- TabWidget tabWidget = tabHost. getTabWidget ();
- TabHost. addTab (tabHost. newTabSpec ("tab1 "). setIndicator ("tab1", getResources (). getDrawable (R. drawable. img01 )). setContent (R. id.1101 ));
- TabHost. addTab (tabHost. newTabSpec ("tab2"). setIndicator ("tab2", getResources (). getDrawable (R. drawable. img02 ))
- . SetContent (R. id.1102 ));
- TabHost. addTab (tabHost. newTabSpec ("tab3"). setIndicator ("tab3", getResources (). getDrawable (R. drawable. img03 ))
- . SetContent (R. id.1103 ));