Implementation of the "Android" tab page, using Tabhost

Source: Internet
Author: User

The first step, the layout file


<?xml version= "1.0" encoding= "Utf-8"? ><tabhost xmlns:android= "Http://schemas.android.com/apk/res/android "Android:layout_width=" match_parent "android:layout_height=" match_parent "android:id=" @android: Id/tabhost "&     Gt <linearlayout android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "V        Ertical "> <include layout=" @layout/top "/> <tabwidget android:layout_width=" Match_parent " android:layout_height= "Wrap_content" android:id= "@android: Id/tabs" ></TabWidget> <fra        Melayout android:layout_width= "match_parent" android:layout_height= "0DP" android:layout_weight= "1" Android:id= "@android: Id/tabcontent" > <linearlayout android:layout_width= "match_pare NT "android:layout_height=" Match_parent "android:id=" @+id/lkgj "android:background=" #ffff FF "Android:orientation= "vertical" android:layout_gravity= "center" > <textview an Droid:layout_width= "250DP" android:layout_height= "wrap_content" android:layout_margintop= "30 DP "android:layout_gravity=" center "android:textsize=" 20DP "android:text=" @st Ring/gjtext "/> <edittext android:layout_width=" 280DP "Android                oid:layout_height= "40DP" android:id= "@+id/lkgjipedittext" android:layout_margintop= "10DP"                  android:layout_gravity= "Center" android:singleline= "true" android:hint= "Please enter IP address" Android:textsize= "15dip" android:textcolorhint= "#AAAAAA" android:layout_margin= "20dip" android:padding= "5dip" android:background= "@drawable/shape" Andro     Id:inputtype= "Text"            /> <button android:id= "@+id/lkgjbtn" android:layout_width= "250d P "android:layout_height=" 80DP "android:layout_gravity=" center "android:layou t_margintop= "40DP" android:text= "@string/lkgjbtn"/> </LinearLayout> &lt ; LinearLayout android:layout_width= "match_parent" android:layout_height= "Match_parent" an            Droid:id= "@+id/dsgj" android:background= "#ffffff" android:orientation= "vertical" >                <textview android:layout_width= "250DP" android:layout_height= "Wrap_content" android:layout_margintop= "30DP" android:layout_gravity= "center" android:textsize= "20DP" android:text= "@string/gjtext"/> <edittext android:l    Ayout_width= "280DP"            android:layout_height= "40DP" android:id= "@+id/dsgjedittext" Android:layout_margi ntop= "10DP" android:layout_gravity= "center" android:singleline= "true" android:hint= "Please enter IP Address "android:textsize=" 15dip "android:textcolorhint=" #AAAAAA "Android:                  Layout_margin= "20dip" android:padding= "5dip" android:background= "@drawable/shape" android:inputtype= "text"/> <edittext android:layout_width= "280d P "android:layout_height=" 40DP "android:id=" @+id/gjdateedittext "Android:layo ut_gravity= "Center" android:singleline= "true" android:hint= "Please enter the number of seconds" android:textsize= " 15dip "android:textcolorhint=" #AAAAAA "android:layout_margin=" 20dip "an    Droid:padding= "5dip"            android:background= "@drawable/shape" android:inputtype= "number"/>            <relativelayout android:layout_width= "match_parent" android:layout_height= "Match_parent"            Android:background= "#ffffff" android:orientation= "vertical" android:layout_gravity= "center" android:gravity= "center" > <button android:layout_width= "150DP" android:layout_height= "80DP" android:id= "@+id/dsgjbtn" android:text= "                 @string/dsgjbtn "android:layout_margintop=" 2DP "/> <button Android:layout_width= "150DP" android:layout_height= "80DP" android:id= "@+id/qxgjb TN "android:layout_torightof=" @+id/dsgjbtn "android:text=" @string/qxgjbtn "an Droid:layout_margintop= "2DP "/> </RelativeLayout> </LinearLayout> & Lt;/framelayout></linearlayout></tabhost>


Step two, initialize the Tabhost


Defining variables at the top


Private Tabhost Tabhost;

In the OnCreate method

From Tabactivity, get the Tabhosttabhost = Gettabhost () of the tab, Tabhost.addtab (Tabhost.newtabspec ("one"). Setindicator (" Shut down immediately "). SetContent (R.ID.LKGJ)); Tabhost.addtab (tabhost//Create a new label One.newtabspec (" both ")//Set the label caption. Setindicator (" timed off ")/ /sets the layout content of the label. SetContent (R.ID.DSGJ));//Click event Tabhost.setontabchangedlistener (New Ontabchangelistener () {<span  Style= "White-space:pre" ></span> @Override <span style= "White-space:pre" ></span> public void ontabchanged (String tabId) {<span style= "white-space:pre" ></span> <span style= "White-spa Ce:pre "></span>} <span style=" White-space:pre "></span>});



Implementation of the "Android" tab page, using Tabhost

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.