Tabwidget is similar to the Android view of the phone book interface, switching through multiple tabs to display different content. To achieve this, you first need to understand tabhost, a container that holds multiple tab tags. Each tab can correspond to its own layout, for example, the phone book in the tab layout is a list of linear layout.
To use Tabhost, you first need to get the Tabhost object by Gettabhost method, and then add Tab to Tabhost by using the AddTab method. Of course each tab in the switch will produce an event, to catch this event needs to set Tabactivity event monitoring Setontabchangedlistener.
1. layout file
<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"/>
<framelayout
Android:id= "@android: Id/tabcontent"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
<textview
android:id= "@+id/textview1" android:layout_width= "fill_parent"
Androi d:layout_height= "Fill_parent"
android:text= "Linux"
android:textcolor= "#FF0000"/> : /span>
<textview
android:id= "@+id/textview2" android:layout_width= "fill_parent"
Androi d:layout_height= "Fill_parent"
android:text= "MAC"
android:textcolor= #385E0F/>
<textview
Android:id= "@+id/textview3"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:text= "Windows"
Android:textcolor= "#1E90FF"/>
</FrameLayout>
</LinearLayout>
</TabHost>
2, modify mainactivity, note is inherited from Tabactivity
public class Mainactivity extends Tabactivity {private Tabhost tabhost;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Tabhost = Gettabhost ();
AddTab ()///Add tags//set tabhost background color Tabhost.setbackgroundcolor (Color.argb (150, 20, 80, 150));
Set Tabhost background picture resource Tabhost.setbackgroundresource (R.drawable.ic_launcher);
To set which label is currently displayed my understanding is that when you first start the program, the default display of that label is here is the ID of the specified tab starting from 0 tabhost.setcurrenttab (0); tab Toggle Event Handling, Setontabchangedlistener Note that the tab switch event is not a click event, but a switch from one label to another will trigger an event Tabhost.setontabchangedlistener (new Ontabchangelistener () {@Override public void ontabchanged (String tabid) {Alertdialog.builder Builder = new ALERTDI Alog.
Builder (Mainactivity.this);
Dialog dia;
Builder.settitle ("hint");
Builder.setmessage ("currently selected" + TabID + "label"); Builder.setpositivebutton (OK), new Onclicklistener () {@Override public void OnClick (dialoginterface dialog, int wh Ich) {Dialog.cancEl ();
}
});
Dia = Builder.create ();
Dia.show ();
}
}); ///Add label to tabhost Create a new newtabsped (new Tabspec) set its label and icon (Setindicator), set content (setcontent)//Tabspec is the Tabhost internal class Tabhost object
The Newtabspec () method returns a Tabspec object//source code inside is so written public tabspec Newtabspec (String tag)//{return new Tabspec (tag); private void AddTab () {Tabhost.addtab (tabhost. Newtabspec ("Tab1"). Setindicator ("TAB1", Getresources (). getdrawabl
E (R.drawable.ic_launcher))//Setindicator () This method is used to set up labels and charts. SetContent (R.id.textview1)); Specifies that the content is a textview--->public tabhost.tabspec setcontent (int viewid) This method requires a viewid as an argument tabhost.addtab (tabhost. New Tabspec ("TaB2"). Setindicator ("TAB2", Getresources (). getdrawable (R.drawable.ic_launcher)). SetContent (
R.ID.TEXTVIEW2)); Tabhost.addtab (Tabhost newtabspec ("Tab3"). Setindicator ("TAB3", Getresources (). Getdrawable (R.drawable.ic_
Launcher)). SetContent (R.ID.TEXTVIEW3));
}
}
3, run the program: as follows!