Android UI learning-how to use Tab

Source: Internet
Author: User
Public void setdefaulttab (string tag) These two functions are easy to understand, that is, to set the default Tab Public 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 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) Add a tab. The tabhost. tabspec parameter is returned using the following function. Public tabhost. tabspec newtabspec (string tag) Create tabhost. tabspec Public void clearalltabs () Remove all tabs Public 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 index Public void setcurrenttabbytag (string tag) Set the current tab by tag Public void setontabchangedlistener (tabhost. ontabchangelistener L) Set Response Processing for tabchanged events Public void setup () This function will be introduced later 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 setindicator I understand what Indicator Is the label on the tab, it can Set label : Setindicator (charsequence label) Or Set label and icon : Setindicator (charsequence label, drawable icon) Or directly Specify a view : Setindicator (view) For Content Is the content in the tab, you can Set the view ID : Setcontent (INT viewid) Or Tabhost. tabcontentfactory To process: Setcontent (tabhost. tabcontentfactory contentfactory) Or use New intent To introduce the content of other activities: Setcontent (intent) Now let's take a look at the official views/tabs/content by ID Example:

Code
 
Public class tabs1 extends tabactivity {@ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); tabhost = gettabhost (); layoutinflater. from (this ). inflate (R. layout. tabs1, tabhost. gettabcontentview (), true); tabhost. addtab (tabhost. newtabspec ("tab1 "). setindicator ("tab1 "). setcontent (R. id. view1); tabhost. addtab (tabhost. newtabspec ("tab3 "). setindicator ("tab2 "). setcontent (R. id. view2); tabhost. addtab (tabhost. newtabspec ("tab3 "). setindicator ("tab3 "). setcontent (R. id. view3 ));}}
After obtaining tabhost, layoutinflater must be used to obtain layout. layoutinflater will be detailed later. R. layout. tabs1 content:
<Framelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: Id = "@ + ID/view1" Android: background = "@ drawable/Blue" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: text = "@ string/tabs_paitab_1"/> <textview Android: id = "@ + ID/view2" Android: Background = "@ drawable/red" Android: l Ayout_width = "fill_parent" Android: layout_height = "fill_parent" Android: text = "@ string/tabs_paitab_2"/> <textview Android: Id = "@ + ID/view3" Android: background = "@ drawable/Green" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: text = "@ string/tabs_1_tab_3"/> </framelayout> <! -- Strings. XML <string name = "tabs_1_tab_1"> tab1 </string> <string name = "tabs_1_tab_2"> tab2 </string> <string name = "tabs_1_tab_3"> tab3 </string> -->
Originally, framelayout was used! For the tab1 content to display tab1 and the background is blue, it is setcontent (R. Id. view1). textview1 is referenced here. Now you can understand how to add a tab and set the label and content. Next let's take a look at the example of views/tabs/content by factory:

Code
 public class tabs2 extends tabactivity implements tabhost. tabcontentfactory {@ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); Final tabhost = gettabhost (); tabhost. addtab (tabhost. newtabspec ("tab1 "). setindicator ("tab1", getresources (). getdrawable (R. drawable. star_big_on )). setcontent (this); tabhost. addtab (tabhost. newtabspec ("tab2 "). setindicator ("tab2 "). setcontent (this); tabhost. addtab (tabhost. newtabspec ("tab3 "). setindicator ("tab3 "). setcontent (this);} public view createtabcontent (string tag) {final textview TV = new textview (this); TV. settext ("content for tab with Tag" + tag); Return TV ;}
You can see that through override rewriting (re-implementation) the method view createtabcontent (string tag) in the parent class tabhost. tabcontentfactory implements different content of different tabs. At the same time, set the setcontent parameter to the corresponding tabcontentfactory. It turns out that createtabcontent is called only when each tab is displayed for the first time. If it is displayed again, it will not be called again. I can view it with logcat! This is critical because createtabcontent is called before the tab is completely created. This means that functions such as getcurrenttabview cannot be called in createtabcontent; otherwise, an error occurs! For the views/tabs/content by intent example, only the code is pasted:
Public class tabs3 extends tabactivity {@ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); Final tabhost = gettabhost (); tabhost. addtab (tabhost. newtabspec ("tab1 "). setindicator ("list "). setcontent (new intent (this, list1.class); tabhost. addtab (tabhost. newtabspec ("tab2 "). setindicator ("Photo List "). setcontent (new intent (this, list8.class); // This tab sets the intent flag so that it is recreated each time // The tab is clicked. tabhost. addtab (tabhost. newtabspec ("tab3 "). setindicator ("Destroy "). setcontent (new intent (this, controls2.class ). addflags (intent. flag_activity_clear_top )));}}
Effect: The content of tab1 is the activity of list1, tab2 is the activity of list8, and tab3 is the activity of controls2.activity.

Tabhost. ontabchangelistener The tabhost. ontabchangelistener interface has only one abstract method ontabchanged (string tagstring). Obviously Ontabchanged (string tagstring) In the method, swtich... case... is used to determine whether to process tagstrings separately. Tabhost. Setup () Here I will post the explanation in the SDK DOC:

Public Void Setup () since: API level 1
Call setup () before adding tabs If Loading tabhost using findviewbyid (). However, you Do Not need to call setup () after gettabhost () in tabactivity. Example:

Mtabhost=(Tabhost) findviewbyid (R. Id. tabhost );
Mtabhost. Setup ();
Mtabhost. addtab (tab_tag_1,"Hello, world!","Tab 1");

// in my opinion, if you want to use findviewbyid to obtain tabhost and add tabs, call setup () before addtab. Public void setup (localactivitymanager activitygroup) since: API level 1
if you are using setcontent (Android. content. intent), This must be called since the activitygroup is needed to launch the local activity. this is done for You If you extend tabactivity.

Parameters
Activitygroup used to launch activitiesForTab content.

Related Article

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.