1,Activity_tabwidget.xml
<?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= "Fill_parent"
android:layout_height= "Fill_parent" >
<linearlayout
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >
<tabwidget
Android:id= "@android: Id/tabs"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" >
</TabWidget>
<framelayout
Android:id= "@android: Id/tabcontent"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
<textview
Android:id= "@+id/TB_TV1 "
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:text= "This is the first tab"/>
<textview
Android:id= "@+id/tb_tv2"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:text= "This is another tab"/>
<textview
Android:id= "@+id/tb_tv3"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:text= "This is the Third tab"/>
</FrameLayout>
</LinearLayout>
</TabHost>
2,Tabwidgetactivity.java
public class Tabwidgetactivity extends Tabactivity {
Declaring Tabhost objects
tabhost tabhost;
@SuppressWarnings ("deprecation")
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_tabwidget);
try {
Get Tabhost Object
tabhost = Gettabhost ();
/ * Add tags to tabhost * /
//Create a new Newtabspec (NEWTABSPEC)
//Set its label and icon (Setindicator)
//Setup content (setcontent)
Tabhost.addtab (tabhost
.Newtabspec("Tab_test1")
.Setindicator("TAB1",
Getresources (). getdrawable (R.drawable.button1))
.setcontent(R.ID.TB_TV1));
Tabhost.addtab (tabhost
. Newtabspec ("Tab_test2")
. Setindicator ("TAB2",
Getresources (). getdrawable (R.drawable.button2))
. SetContent (R.ID.TB_TV2));
Tabhost.addtab (Tabhost.newtabspec ("Tab_test3"). Setindicator ("TAB3",
Getresources (). getdrawable (R.drawable.button3)). SetContent (R.ID.TB_TV3));
Set the background color of tabhost
Tabhost.setbackgroundcolor (Color.argb (150, 22, 70, 150));
Set the background image resource for Tabhost
Tabhost.setbackgroundresource (R.drawable.ic_launcher);
Set the currently displayed label
Tabhost.Setcurrenttab(0);
} catch (Exception e) {
E.printstacktrace ();
}
Label Switching event handling
Tabhost.Setontabchangedlistener(New Ontabchangelistener () {
@Override
public void ontabchanged (String tabId) {
Dialog Dialog = new Alertdialog.builder (tabwidgetactivity.this)
. Settitle ("hint")
. Setmessage ("currently checked" + tabId + "label")
. Setpositivebutton ("OK",
New Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog,
int Whichbutton) {
Dialog.cancel ();
}
}). Create ();
Dialog.show ();
}
});
}
}
Android--tabwidget