1. Concept
The container in which the tab is placed is tabhost. There are two ways to implement Tabhost:
The first inherits Tabactivity, which gets tabhost from Tabactivity with the Gettabhost () method. The contents of each tab are defined in the layout file.
The second way, do not inherit tabactivity, define Tabhost in the layout file, but the ID of tabwidget must be @android:id/tabs,framelayout ID must be @android:id/ Tabcontent.
2. Case studies
1) Inherit tabactivity
Res/layout/activity_main.xml
<?xml version= "1.0" encoding= "Utf-8"?> <!--define Tabhost components--<tabhost xmlns:android= "/http/ Schemas.android.com/apk/res/android "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "> <!--define the contents of the first tab--<linearlayout android:id= "@+id/tab01"
android:orientation= "Vertical"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" > <!--define a TextView to display the contents of the tab--
<textview
android:text= "Test"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/> </LinearLayout> <!--define the contents of the Second tab--<linearlayout
Android:id= "@+id/tab02"
android:orientation= "Vertical"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" > <!--is too lazy, there is no content--
</LinearLayout> <!--Define the contents of the Third tab--<linearlayout
Android:id= "@+id/tab03"
android:orientation= "Vertical"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" > <!--is too lazy, there is no content--
</LinearLayout>
Main.java
public class Main extends tabactivity {@Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Call Tabactivity's Gettabhost () method to get the Tabhost object tabhost tabhost = Gettabhost (); Settings use Tabhost layout Layoutinflater.from (this). Inflate (R.layout.main, Tabhost.gettabcontentview (), true); Add the first tab of Tabhost.addtab (Tabhost.newtabspec ("Tab01"). Setindicator ("xxx"). SetContent (R.ID.TAB01)); Add a second tab and add a picture Tabhost.addtab (Tabhost.newtabspec ("TAB02") to its label. Setindicator ("xxxx", Getresources (). Getdrawable (R.drawable.icon)). SetContent (R.ID.TAB02)); Add a third tab Tabhost.addtab (Tabhost.newtabspec ("tab03"). Setindicator ("xxxxxxx"). SetContent (R.ID.TAB03)); } }
2) do not inherit tabactivity
Inherit common activity,<tabwidget> tag ID must be tabs, <FrameLayout> The label ID must be tabcontent. This method must be called by the Setup method after Tabhost is obtained through Findviewbyid.
Res/layout/activity_main.xml Code
<?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:orientation=" Vertical "Android Oid:id= "@+id/tabhost" > <relativelayout android:layout_width= "match_parent" android:layout_height= "M Atch_parent "> <tabwidget android:id=" @android: Id/tabs "Android:layout_alignparentbottom = "true" android:layout_width= "Match_parent" android:layout_height= "wrap_content"/> <fra Melayout android:id= "@android: Id/tabcontent" android:layout_above= "@android: Id/tabs" Android Oid:layout_width= "Match_parent" android:layout_height= "match_parent" > <linearlayout Android:id= "@+id/tab01" android:orientation= "vertical" android:layout_width= "match_pare NT "Android:layout_height= "Match_parent" > <searchview android:layout_width= "match_parent" android:layout_height= "Wrap_content" > </SearchView> <scrollview Android:layout_width= "Match_parent" android:layout_height= "Match_parent" > <linearlayout android:orientation= "vertical" android:layout_width= "m Atch_parent "android:layout_height=" wrap_content "> <textview Android:id= "@+id/text03" android:text= "in Design" Android Oid:layout_width= "Match_parent" android:layout_height= "Wrap_content"/> & lt;/linearlayout> </ScrollView> </LinearLayout> <linearlayout Android:id= "@+ID/TAB02 "android:orientation=" vertical "android:layout_width=" match_parent " android:layout_height= "Match_parent" > <textview android:layout_width= "wrap_content "Android:layout_height=" wrap_content "android:text=" Please look forward to "/> </line arlayout> <linearlayout android:id= "@+id/tab03" android:orientation= "vertic Al "Android:layout_width=" match_parent "android:layout_height=" Match_parent "> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_con Tent "android:text="/> </LinearLayout> <linearlayout Android:id= "@+id/tab04" android:orientation= "vertical" android:layout_width= "Match_paren T "Android:layout_height= "Match_parent" > <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "please expect"/> </linearlayo Ut> </FrameLayout> </RelativeLayout></TabHost>
Main.java
public class Main extends Activity { @Override protected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Set Tabhost settabhost (); set Tabhost, to set the picture contents, refer to above protected void Settabhost () { tabhost tabhost = (tabhost) Findviewbyid ( R.id.tabhost); Tabhost.setup (); Tabhost.addtab (Tabhost.newtabspec ("Tab1"). Setindicator ("xxx") . SetContent (R.ID.TAB01)); Tabhost.addtab (Tabhost.newtabspec ("TaB2"). Setindicator ("xxxx") . SetContent (R.ID.TAB02)); Tabhost.addtab (Tabhost.newtabspec ("Tab3"). Setindicator ("xxxxx") . SetContent (R.ID.TAB03)); Tabhost.addtab (Tabhost.newtabspec ("Tab4"). Setindicator ("xxxx") . SetContent (R.ID.TAB04));} }
Current Problems and Solutions
1, to put down the navigation bar How to do, more lazy, directly put a project inside the paste up, deleted part of the use of
<?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:orientation=" Vertical "Android Oid:id= "@+id/tabhost" > <relativelayout android:layout_width= "match_parent" android:layout_height= "M Atch_parent "> <tabwidget android:id=" @android: Id/tabs "
<!--see no, here, directly set at the bottom--android:layout_alignparentbottom= "true" Android:layout_width= "Match_parent" android:layout_height= "wrap_content"/> <framelayou T android:id= "@android: Id/tabcontent" android:layout_above= "@android: Id/tabs" Android:lay Out_width= "Match_parent" android:layout_height= "match_parent" > <linearlayout an Droid:id= "@+id/tab01" android:orientation= "vertical" android:layout_width= "match_parent" android:layout_height= "Match_parent" > </LinearLayout> <linearlayout Android:id= "@+id/tab02" android:orientation= "vertical" android:layout_width= "Match_pa Rent "android:layout_height=" match_parent "> <textview android:layou T_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "please expect"/> </linearlayout> <linearlayout android:id= "@+id/tab03" android:orientation= "vertical" Android:layout_width= "Match_parent" android:layout_height= "Match_parent" > <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_conten T "android:text=" please expect "/> </LinearLayout> <linearlayout Android:id= "@+id/tab04" android:orientation= "vertical" android:layout_width= "match_parent" android:layout_height= "Match_parent" > <textview Android:layout_widt H= "Wrap_content" android:layout_height= "wrap_content" android:text= "please expect"/> </LinearLayout> </FrameLayout> </RelativeLayout></TabHost>
2, Input method to the top of the tab how to do
Add a word to Androidmanifest.xml's activity
Android:windowsoftinputmode= "Statevisible|adjustpan"
Android's Tabhost layout