Use the [Android] Tab

Source: Internet
Author: User

1. Create three activity classes

Mainactiviey. Java

Package tianshuai. home_page; </P> <p> Import android. app. activity; <br/> Import android. app. activitygroup; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. widget. tabhost; <br/> Import android. widget. tabhost. tabspec; </P> <p> public class mainactivity extends activitygroup implements view. onclicklistener <br/>{< br/> Public static tabhost tab_host; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) <br/>{< br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> createtab (); <br/>}</P> <p> Public void createtab () <br/> {<br/> tab_host = (tabhost) findviewbyid (R. id. tab_host); <br/> tab_host.setup (this. getlocalactivitymanager (); </P> <p> tab_host.setonclicklistener (this); </P> <p> tabspec ts1 = tab_host.newtabspec ("cell phone site "); <br/> ts1.setindicator ("playing", getresources (). getdrawable (R. drawable. find); <br/> ts1.setcontent (new intent (this, cellphone_web.class); </P> <p> tab_host.addtab (ts1 ); </P> <p> tabspec ts2 = tab_host.newtabspec ("software game"); <br/> ts2.setindicator ("Local Database", getresources (). getdrawable (R. drawable. history); <br/> ts2.setcontent (new intent (this, software. class); </P> <p> tab_host.addtab (ts2); <br/> tab_host.setcurrenttab (1 ); <br/>}</P> <p> Public void onclick (view arg0) {<br/> throw new unsupportedoperationexception ("not supported yet. "); <br/>}</P> <p>}
2. cellphone_web.java and software. Java are empty classes that inherit activity.

3. Main. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> </P> <p> <tabhost <br/> Android: id = "@ + ID/tab_host" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <linearlayout <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: padding = "5dp"> <br/> <framelayout <br/> Android: Id = "@ Android: ID/tabcontent "<br/> Android: layout_width =" fill_parent "<br/> Android: layout_height =" wrap_content "<br/> Android: padding = "5dp" <br/> Android: layout_weight = "1"/> <br/> <tabwidget <br/> Android: Id = "@ Android: ID/tabs "<br/> Android: layout_width =" fill_parent "<br/> Android: layout_height =" wrap_content "<br/> Android: layout_weight = "0"/> <br/> </linearlayout> <br/> </tabhost> <br/> </linearlayout> <br/>
Note:Tabwidget and framelayout have different ID namespaces Android: Id = "@ Android: ID/idnames". This is required. Therefore, tabhost can be automatically found. The activity must inherit tabactivity.

If you want the tab to be above:

<Framelayout <br/> Android: Id = "@ Android: ID/tabcontent" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: padding = "5dp" <br/> Android: layout_weight = "0"/> <br/> <tabwidget <br/> Android: Id = "@ Android: ID/tabs" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_weight = "1"/>If the content is above, tabwidget must change the position with framelayout.

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.