[Android Basics] Three Ways to add a tab in the TabHost navigation bar: androidtabhost

Source: Internet
Author: User

[Android Basics] Three Ways to add a tab in the TabHost navigation bar: androidtabhost

There are three ways to add pages in the navigation bar implemented by using TabHost: adding pages directly in the tab of the layout Code, adding pages using the include tag, and adding pages using the Java code to specify another Activity. The following is an example of three methods:

Directly add a tab in the tab of the layout code

The TabHost dragged out by the Drag Control has three LinearLayout layout IDs at the layout level: tab1, tab2, and tab3, as shown in:

Then we can create a new Layout. After designing the interface in this Layout, we can directly add the xml code to the tab (1, 2, 3) label, as shown in, put the designed text label in the tab1 label:

This method is relatively simple and easy to understand, but it is only suitable for small application interfaces. If the application interface is very complex, the layout file will become very large, which is not conducive to code maintenance.

Use the include label instead of layout

Include labels can load a layout file, embedded in another layout file, very helpful to split large layout files, this is my article on the use of include labels blog, http://blog.csdn.net/zgljl2012/article/details/44665643
At this point, this method should be better understood, that is, first design a layout file, and then use the include tag to embed it into a tag. I will not go into details here.

Use the addTab (TabSpec tabSpec) method to add a tab

MTabHost is the TabHost control. Then, newTabSpec is used to create a TabSpec and the name is "Page 3". The Activity is Tab3:

MTabHost. addTab (mTabHost. newTabSpec ("tab3"). setIndicator ("Page 3"). setContent (new Intent (this, Tab3.class )));

We recommend that you use the second and third methods, the second method for static tab display, and the third method for tabs that need to interact with users.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.