tab and Tabhost:
This is tab, and the container for the tab is tabhost.
How to achieve??
Each tab also corresponds to a layout, this is a bit of fun. An activity that corresponds to a plurality of functional layouts.
Create a new tab item, and note that you do not generate main activity.
Notice the IDE, do not choose here ...
Create a new class Mytab in the package, inheriting from Tabactivity.
In fact, Tabactivity is a subclass of activity.
Package zyf.tab.test;
Import android.app.TabActivity;
public class Mytab extends Tabactivity {
}
Inherit the OnCreate () entry method from the parent class
Package zyf.tab.test;
Import android.app.TabActivity;
Import Android.os.Bundle;
public class Mytab extends Tabactivity {
@Override
protected void onCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
super.oncreate (Savedinstancestate);
}
Register the Mytab Class (activity) in the Manifest.xml file
<activity android:name= ". Mytab ">
<intent-filter>
<action android:name=" Android.intent.action.MAIN "></action >
<category android:name= "Android.intent.category.LAUNCHER" ></category>
</ Intent-filter>
</activity>
At this time, need to design the corresponding layout of the label page, generally using framelayout as the root layout, each tab page corresponding to a child node layout
<?xml version= "1.0" encoding= "Utf-8"?> <!--this is the root node layout--> <framelayout xmlns:android= "http://"
Schemas.android.com/apk/res/android "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "> <!--the first tab corresponding to the layout--> <linearlayout android:id= "@+id/widget_layout_blue" android:layout_width= fill_parent "Android:layout_height=" fill_parent "androidrientation=" vertical "> <edittext android:id=" @+id/widget34 "an Droid:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:text= "EditText" android:textSize = "18SP" > </EditText> <button android:id= "@+id/widget30" android:layout_width= "Wrap_content" Andr oid:layout_height= "wrap_content" android:text= "button" > </Button> </LinearLayout> <!--the second tab corresponding to the cloth Bureau--> <linearlayout android:id= "@+id/widget_layout_red" android:layout_width= "Fill_parent" Android:layout_hei ght= "Fill_parent" androidrientation= "veRtical "> <analogclock android:id=" @+id/widget36 "android:layout_width=" Wrap_content "Android:layout_heigh" t= "Wrap_content" > </AnalogClock> </LinearLayout> <!--the third tab corresponding to the layout--> <linearlayout Andr Oid:id= "@+id/widget_layout_green" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" Androidr ientation= "vertical" > <radiogroup android:id= "@+id/widget43" android:layout_width= "166px" Android:layout_h eight= "98px" androidrientation= "vertical" > <radiobutton android:id= "@+id/widget44" android:layou T_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "RadioButton" > </radiobutton&
Gt <radiobutton android:id= "@+id/widget45" android:layout_width= wrap_content "android:layout_height=" Wrap_conten T "android:text=" RadioButton "> </RadioButton> </RadioGroup> </LinearLayout> </
Framelayout>
First, you should declare tabhost, then filter out the layout with Layoutinflater, and add tabhost with the tab page Framelayout
Private Tabhost mytabhost;
Mytabhost=this.gettabhost ()///Tabactivity the Tabhost layoutinflater.from (this) where tab is placed
. Inflate ( R.layout.main, Mytabhost.gettabcontentview (), true);
From (this) gets the Layoutinflater//r.layout.main the tab layout from the tabactivity or
Tabhost gets the contents of the Tab tab page framelayout
//Whether the inflate tied to the root layout elements on the
Mytabhost.setbackgroundcolor (Color.argb ();
Set the color of the Tabhost
Next, create a label in Tabhost, and then set the title/icon/label page layout
Mytabhost.addtab (Mytabhost.newtabspec ("TT")//Create a new label TT
. Setindicator ("KK", Getresources (). Getdrawable ( R.DRAWABLE.AJJC)
//Set the display title for KK, set the label icon for AJJC
. SetContent (r.id.widget_layout_red));
Set the layout of the tab page to R.id.widget_layout_red, which is a child of the framelayout layout
Label Toggle event Handling, Setontabchangedlistener
Mytabhost.setontabchangedlistener (New Ontabchangelistener () {
@Override public
void Ontabchanged (String TabID) {
//TODO auto-generated method stub
}
});
Dynamic menu for each label page
First, you put the menu you designed in XML into an int array.
private static final int mymenuresources[] = {r.menu.phonebook_menu,
r.menu.addphone_menu, R.menu.chatting_menu, R.menu.userapp_menu};
Set the Mymenusettingtag in the Setontabchangedlistener () method based on the switch of the label
Override public
void ontabchanged (String tagstring) {
//TODO auto-generated method stub
if ( Tagstring.equals ("one")) {
Mymenusettingtag = 1;
}
if (Tagstring.equals ("two")) {
Mymenusettingtag = 2;
}
if (Tagstring.equals ("Three")) {
Mymenusettingtag = 3;
}
if (Tagstring.equals ("Four")) {
Mymenusettingtag = 4;
}
if (mymenu!= null) {
oncreateoptionsmenu (mymenu);
}
}
The menu is then dynamically added through the Menuinflater filter in the Oncreateoptionsmenu (menu) method
@Override Public
Boolean oncreateoptionsmenu (Menu menu) {
//TODO auto-generated a stub
//Hold on to Thi s
mymenu = menu;
Mymenu.clear ()//Empty menu
//Inflate the currently selected menu XML resource.
Menuinflater inflater = Getmenuinflater ();
Get a menu filter
switch (mymenusettingtag) {case
1:
inflater.inflate (mymenuresources[0) from tabactivity , menu);
Dynamically joins the corresponding XML menu break in the array
;
Case 2:
inflater.inflate (mymenuresources[1], menu);
break;
Case 3:
inflater.inflate (mymenuresources[2], menu);
break;
Case 4:
inflater.inflate (mymenuresources[3], menu);
break;
Default: Break
;
}
return Super.oncreateoptionsmenu (menu);
Menu layout
<?xml version= "1.0" encoding= "Utf-8"?> <menu xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
<group android:id=" @+id/group_a "><item android:id=" @+id/item_a "android:icon=" @drawable /gimp "android:title=" gimp ></item>
</group>
</menu>
Run effect
Imitation micro-Signal navigation Example:
<?xml version= "1.0" encoding= "Utf-8"?> <tabhost xmlns:android= "Http://schemas.android.com/apk/res/android"
"Android:id=" @android: Id/tabhost "android:layout_width=" match_parent "android:layout_height=" Match_parent "> <linearlayout android:layout_width= "match_parent" android:layout_height= "Match_parent" Android:orie ntation= "vertical" > <framelayout android:id= "@android: Id/tabcontent" Android:layout_width= "mat
Ch_parent "android:layout_height=" 0.0dip "android:layout_weight=" 1.0 "> </FrameLayout> <tabwidget android:id= "@android: Id/tabs" android:layout_width= "Match_parent" android:layout_height = "Wrap_content" android:visibility= "Gone" > </TabWidget> <radiogroup android:layout_ Width= "Match_parent" android:layout_height= "wrap_content" android:layout_gravity= "Bottom" Android:bac Kground= "@android: Color/black"
android:orientation= "Horizontal" > <radiobutton android:id= "@+id/talk" @st Yle/rbt_bottom "android:drawabletop=" @drawable/take_bottom "android:text=" @string/talk "/> & Lt RadioButton android:id= "@+id/address" style= "@style/rbt_bottom" android:drawabletop= "@drawable
Adrress_bottom "android:text=" @string/address "/> <radiobutton android:id=" @+id/find " style= "@style/rbt_bottom" android:drawabletop= "@drawable/find_bottom" android:text= "@string/find" /> <radiobutton android:id= "@+id/me" style= "@style/rbt_bottom" Android:drawablet op= "@drawable/me_bottom" android:text= "@string/me"/> </RadioGroup> </LinearLayout>
;/tabhost>
<?xml version= "1.0" encoding= "Utf-8"?> <selector "xmlns:android=" Schemas.android.com/apk/res/android "> <item android:drawable=" @drawable/n_address_l "android:state_checked=
"True" android:state_enabled= "true"/> <item android:drawable= "@drawable/n_address_h"/> </selector>
Package com.android.xiong.bkclient;
Import android.app.TabActivity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.widget.CompoundButton;
Import Android.widget.CompoundButton.OnCheckedChangeListener;
Import Android.widget.RadioButton;
Import Android.widget.TabHost;
@SuppressWarnings ("deprecation") public class Mainactivity extends Tabactivity implements Oncheckedchangelistener {
Private Tabhost Tabhost;
Private Intent addressintent;
Private Intent meintent;
Private Intent takeintent;
Private Intent findintent;
Private RadioButton FINDBT;
Private RadioButton ADDRESSBT;
Private RadioButton meBt;
Private RadioButton takebt;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.tabhostmain);
Addressintent = new Intent (this, addressactivity.class);
Meintent = new Intent (this, meactivity.class); Takeintent = new IntenT (this, takeactivity.class);
Findintent = new Intent (this, findactivity.class);
FINDBT = (RadioButton) Findviewbyid (R.id.find);
ADDRESSBT = (RadioButton) Findviewbyid (r.id.address);
MeBt = (RadioButton) Findviewbyid (r.id.me);
TAKEBT = (RadioButton) Findviewbyid (R.id.talk);
Tabhost =gettabhost ();
Tabhost.addtab (Tabhost.newtabspec ("take"). Setindicator ("Take"). SetContent (takeintent));
Tabhost.addtab (Tabhost.newtabspec ("Address"). Setindicator ("Address"). SetContent (addressintent));
Tabhost.addtab (Tabhost.newtabspec ("find"), Setindicator ("find"). SetContent (findintent));
Tabhost.addtab (Tabhost.newtabspec ("Me"). Setindicator ("Me"). SetContent (meintent));
Findbt.setoncheckedchangelistener (this);
Mebt.setoncheckedchangelistener (this);
Takebt.setoncheckedchangelistener (this);
Addressbt.setoncheckedchangelistener (this); @Override public void oncheckedchanged (Compoundbutton view,Boolean ischeak) {if (ischeak) {switch (View.getid ()) {case R.id.talk:tabhost.setcurrentta
Bbytag ("take");
Break
Case R.id.find:tabhost.setcurrenttabbytag ("find");
Break
Case R.id.me:tabhost.setcurrenttabbytag ("Me");
Break
Case R.id.address:tabhost.setcurrenttabbytag ("address");
Break
Default:break;
}
}
}
}
<manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= "Com.android.xiong.bkclient" Andr Oid:versioncode= "1" android:versionname= "1.0" > <uses-sdk android:minsdkversion= "8" Android:target
Sdkversion= "/>" <application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" Android:label= "@string/app_name" android:theme= "@style/apptheme" > <activity android:name= "Com.android.xio Ng.bkclient.MainActivity "> <intent-filter> <action android:name=" Android.intent.action.MAIN " > <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> < /activity> <activity android:name= "com.android.xiong.bkclient.AddressActivity" ></activity> <ac Tivity android:name= "com.android.xiong.bkclient.FindActivity" ></activity> <activity android:name= " Com.android.xiong.bkclient.MeAcTivity "></activity> <activity android:name=" Com.android.xiong.bkclient.TakeActivity "></
Activity> </application> </manifest>