[Android] custom tabhost

Source: Internet
Author: User

:

Mainactivity. JavaPackage tianshuai. homepage; </P> <p> Import android. app. tabactivity; <br/> Import android. OS. bundle; <br/> Import android. view. layoutinflater; <br/> Import android. widget. relativelayout; <br/> Import android. widget. tabhost; <br/> Import android. widget. tabhost. tabspec; <br/> Import android. widget. textview; </P> <p> public class mainactivity extends tabactivity <br/> {<br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) <br/>{< br/> super. oncreate (savedinstancestate); </P> <p> // obtain the current tabhost <br/> tabhost tabs = gettabhost (); </P> <p> // load the main layout file <br/> layoutinflater. from (this ). inflate (R. layout. main, tabs. gettabcontentview (), true); </P> <p> // Add a custom style to tab1 <br/> relativelayout tabstyle1 = (relativelayout) layoutinflater. from (this ). inflate (R. layout. tab_style, null); <br/> textview text1 = (textview) tabstyle1.findviewbyid (R. id. tab_label); <br/> text1.settext ("1"); </P> <p> // Add a custom style to tab2 <br/> relativelayout tabstyle2 = (relativelayout) layoutinflater. from (this ). inflate (R. layout. tab_style, null); <br/> textview text2 = (textview) tabstyle2.findviewbyid (R. id. tab_label); <br/> text2.settext ("2"); </P> <p> // create a new tab and use the tab1 style <br/> tabspec tab1 = tabs. newtabspec ("tab1"); <br/> tab1.setindicator (tabstyle1); <br/> tab1.setcontent (R. id. tab1); <br/> tabs. addtab (tab1); </P> <p> // create a new tab and use the tab2 style <br/> tabspec tab2 = tabs. newtabspec ("tab2"); <br/> tab2.setindicator (tabstyle2); <br/> tab2.setcontent (R. id. tab2); <br/> tabs. addtab (tab2); <br/>}</P> <p>}Main. xml<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <framelayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> </P> <p> <linearlayout <br/> Android: Id = "@ + ID/tab1" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "fill_parent" <br/> Android: orientation = "vertical" <br/> </P> <p> <textview <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "this is tab1" <br/> </linearlayout> </P> <p> <linearlayout <br/> Android: id = "@ + ID/tab2" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "fill_parent" <br/> Android: orientation = "vertical" <br/> </P> <p> <textview <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "this is tab2" <br/> </linearlayout> <br/> </framelayout>

Table_style.xml<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: paddingleft = "5dip" <br/> Android: paddingright = "5dip" </P> <p> <textview Android: Id = "@ + ID/tab_label" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "30px" <br/> Android: gravity = "center" <br/> Android: textcolor = "#000000" <br/> Android: textstyle = "bold" <br/> Android: background = "@ drawable/tab" <br/> </relativelayout>Tab. xml<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <selector xmlns: Android = "http://schemas.android.com/apk/res/android"> </P> <p> <item <br/> Android: state_selected = "true" <br/> Android: drawable = "@ drawable/red" <br/> <item <br/> Android: state_selected = "false" <br/> Android: drawable = "@ drawable/bule" <br/> </selector>

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.