Learn android <TabHost tag component. Twenty-nine ..> from scratch, androidtabhost
The main feature of TabHost is that the content of multiple tag columns can be displayed in a window. In Android, each tag column is called a Tab, containers that contain these label columns call it TabHost. The Inheritance structure of the TabHost class is as follows: java. lang. object upload android. view. view every android. view. viewGroup uses android. widget. frameLayout extends android. widget. tabHost
The common method is as follows:
1 |
Public TabHost (Context context) |
Structure |
Create a TabHost Class Object |
2 |
Public void addTab (TabHost. TabSpec tabSpec) |
Normal |
Add a Tab |
3 |
Public TabHost. TabSpec newTabSpec (String tag) |
Normal |
Create a TabHost. TabSpec object |
4 |
Public View getCurrentView () |
Normal |
Obtains the current View object. |
5 |
Public void setup () |
Normal |
Create a TabHost object |
6 |
Public void setCurrentTab (int index) |
Normal |
Set the currently displayed Tab number |
7 |
Public void setCurrentTabByTag (String tag) |
Normal |
Set the name of the currently displayed Tab |
8 |
Public FrameLayout getTabContentView () |
Normal |
Return tag container |
9 |
Public void setOnTabChangedListener (TabHost. OnTabChangeListener l) |
Normal |
Triggered when the tag is set to change |
Two ways to implement TabHost
Method 1:Directly let an Activity program inherit the TabActivity class;
Method 2:Use the findViewById () method to obtain the TagHost component and perform some configuration;
First, let a class inherit tabActivity
XMl file configuration requires nesting in an xml file and layout to display different content in different tabs.
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <LinearLayout android: layout_marginTop = "50dp" android: id = "@ + id/login" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TableRow android: id = "@ + id/tableRow1" android: layout_width = "match_parent" android: layout_height = "wrap_content"> <TextView android: id = "@ + id/textView1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Account"/> <EditText android: id = "@ + id/editText1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: EMS = "10" android: textSize = "25sp"/> </TableRow> <TableRow android: id = "@ + id/tableRow2" android: layout_width = "match_parent" android: layout_height = "wrap_content"> <TextView android: id = "@ + id/textView2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "password"/> <EditText android: id = "@ + id/editText2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: EMS = "10" android: textSize = "25sp"/> </TableRow> <TableRow android: id = "@ + id/tableRow3" android: layout_width = "match_parent" android: layout_height = "wrap_content"> <Button android: id = "@ + id/button1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "cancel"/> <Button android: id = "@ + id/button2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "login"/> </TableRow> </LinearLayout> <LinearLayout android: layout_marginTop = "50dp" android: id = "@ + id/image" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <ImageView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: src = "@ drawable/a2"/> </LinearLayout> <LinearLayout android: layout_marginTop = "50dp" android: id = "@ + id/timer" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TimePicker android: layout_width = "match_parent" android: layout_height = "wrap_content"/> </LinearLayout> <LinearLayout android: layout_marginTop = "50dp" android: id = "@ + id/timer1" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TimePicker android: layout_width = "match_parent" android: layout_height = "wrap_content"/> </LinearLayout> <LinearLayout android: layout_marginTop = "50dp" android: id = "@ + id/timer2" android: layout_width = "match_parent" android: layout_height = "inherit" android: orientation = "vertical"> <TimePicker android: layout_width = "match_parent" android: layout_height = "wrap_content"/> </LinearLayout>
JAVA File Settings
Package com. example. tabhost; import android. app. tabActivity; import android. OS. bundle; import android. view. layoutInflater; import android. widget. tabHost; import android. widget. tabHost. tabSpec; public class MainActivity extends TabActivity {private TabHost tabHost; // initialize the TabHost component private int reslayout [] = {R. id. login, R. id. image, R. id. timer, R. id. timer1, R. id. timer2}; // set the corresponding xml file private int images [] = {R. drawable. cart, R. drawable. cloud, R. drawable. comment, R. drawable. gear, R. drawable. joystick}; // set the displayed title file @ SuppressWarnings ("deprecation") @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); this. tabHost = super. getTabHost (); // instantiate the TabHost component // obtain the LayoutInflater object LayoutInflater. from (this ). inflate (R. layout. linearlayout, // set the layout this. tabHost. getTabContentView (), // specify the container added to the label as true); for (int I = 0; I <reslayout. length; I ++) {TabSpec myTab = tabHost. newTabSpec ("tab" + I); // defines TabSpecmyTab. setIndicator (null, getResources (). getDrawable (images [I]); // you can specify the myTab label. setContent (this. reslayout [I]); // sets the displayed component this. tabHost. addTab (myTab );}}}
As follows:
Use the configuration file settings. This method is more common. You can say that TabHost is placed at the bottom.
However, the layout file is complex. You can refer to the example to learn more.
XML file
<? Xml version = "1.0" encoding = "UTF-8"?> <TabHost xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id/tabhost" android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <RelativeLayout android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <TabWidget android: id = "@ android: id/tabs "android: layout_width =" fill_parent "android: layout_height =" wrap_content "android: layout_alignParentBottom =" true "/> <FrameLayout android: id =" @ android: id/tabcontent "android: layout_width =" fill_parent "android: layout_height =" fill_parent "> <LinearLayout android: id =" @ + id/login "android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TableRow android: id = "@ + id/tableRow1" android: layout_width = "match_parent" android: layout_height = "wrap_content"> <TextView android: id = "@ + id/textView1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Account"/> <EditText android: id = "@ + id/editText1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: EMS = "10" android: textSize = "25sp"/> </TableRow> <TableRow android: id = "@ + id/tableRow2" android: layout_width = "match_parent" android: layout_height = "wrap_content"> <TextView android: id = "@ + id/textView2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "password"/> <EditText android: id = "@ + id/editText2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: EMS = "10" android: textSize = "25sp"/> </TableRow> <TableRow android: id = "@ + id/tableRow3" android: layout_width = "match_parent" android: layout_height = "wrap_content"> <Button android: id = "@ + id/button1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "cancel"/> <Button android: id = "@ + id/button2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "login"/> </TableRow> </LinearLayout> <LinearLayout android: id = "@ + id/image" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <ImageView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: src = "@ drawable/a2"/> </LinearLayout> <LinearLayout android: id = "@ + id/timer" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TimePicker android: layout_width = "match_parent" android: layout_height = "wrap_content"/> </LinearLayout> <LinearLayout android: id = "@ + id/timer1" android: layout_width = "match_parent" android: layout_height = "inline" android: orientation = "vertical"> <TimePicker android: layout_width = "match_parent" android: layout_height = "wrap_content"/> </LinearLayout> <LinearLayout android: id = "@ + id/timer2" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TimePicker android: layout_width = "match_parent" android: layout_height = "wrap_content"/> </LinearLayout> </FrameLayout> </RelativeLayout> </TabHost>
JAVA file configuration
Package com. example. tabhost; import android. OS. bundle; import android. app. activity; import android. widget. tabHost; import android. widget. tabHost. tabSpec; public class MainActivity extends Activity {// directly inherits Activityprivate TabHost myTabHost; // defines TabHostprivate int [] layRes = {R. id. login, R. id. image, R. id. timer, R. id. timer1, R. id. timer2}; // defines the embedded layout manager IDprivate int images [] = {R. drawable. cart, R. drawable. cloud, R. drawable. comment, R. drawable. gear, R. drawable. joystick}; // The title image data @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); super. setContentView (R. layout. activity_main); // call the default layout manager this. myTabHost = (TabHost) super. findViewById (R. id. tabhost); // obtain the TabHost object this. myTabHost. setup (); // create a TabHost object for (int x = 0; x <this. layRes. length; x ++) {// cyclically retrieve all layout tags TabSpec myTab = myTabHost. newTabSpec ("tab" + x); // defines TabSpecmyTab. setIndicator (null, getResources (). getDrawable (images [x]); // sets the label text myTab. setContent (this. layRes [x]); // sets the displayed component this. myTabHost. addTab (myTab); // Add Tag} this. myTabHost. setCurrentTab (0); // set start index }}
Setting the combination of Tab switching and intent using the TabHost component is often used in development and is the basis of the app development framework.
Next prediction: Menu
Android TabHost label transparency how to make the labels in TabHost transparent, including the controls
Do you want to make tabhost more beautiful?
You can use a custom tabhost. I have a Demo here. You can send it to you.
In android, how does one display a tabhost?
The Tab is a frequently used interface control during interface design. It enables fast switching between multiple pages. Different content can be displayed on each page. It is a built-in Tab Of the Android system, click "outbound call/access key" to display the call and display dialing records and contacts.
Use of the Tab
Design the page layout first
After the paging design is complete, use the code to create a Tab, and add the logo and title to each Tab.
Finally, determine the page layout displayed on each page.
Create an XML file for each page to edit and save the page layout. The method used is no different from the method used to design a common user interface.
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 IDS as layout01, layout02, and layout03 respectively.
The key code is as follows:
10. public void onCreate (Bundle savedInstanceState ){
11. super. onCreate (savedInstanceState );
12. TabHost tabHost = getTabHost ();
13. LayoutInflater. from (this). inflate (R. layout. tab1, tabHost. getTabContentView (), true );
14. LayoutInflater. from (this). inflate (R. layout. tab2, tabHost. getTabContentView (), true );
15. LayoutInflater. from (this). inflate (R. layout. tab3, tabHost. getTabContentView (), true );
16. tabHost. addTab (tabHost. newTabSpec ("TAB1 ")
17. setIndicator ("linear layout"). setContent (R. id. layout01 ));
18. tabHost. addTab (tabHost. newTabSpec ("TAB2 ")
19. setIndicator ("absolute layout"). setContent (R. id. layout02 ));
20. tabHost. addTab (tabHost. newTabSpec ("TAB3 ")
21. setIndicator ("relative layout"). setContent (R. id. layout03 ));
22 .}
23 .}
The TabDemo class of the 8th-line code inherits from TabActivity, which is different from the previous inherited Activity. TabActivity supports embedding multiple activities or views
The 12th line of code obtains the container of the Tab by using the getTabHost () function to carry the Tab labels that can be clicked and the page layout.
13th line of code uses LayoutInflater to convert the layout in the tab1.xml file to the View object that can be used by the Tab
The Code in line 16th adds 1st pages using the addTab () function, tabHost. newTabSpec ("TAB1") indicates that a Tab page marked as TAB1 is added to the tabHost created in the 12th line code.
17th lines of code use s... the remaining full text>