At that time, the customer needed to implement the Tabhost title to dynamically display the number obtained from the database. At first, it is very simple to get the number from the database, but to display the number of fetches in the Tabhost header, after thinking, and want to use handler to asynchronously implement message delivery.
If you set the icon or caption variable to a global variable, you should know my purpose. That is, in order to continue to assign values in this class.
Let's start with a simple understanding of tabhost.
The file code for the 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:orientation="Vertical"Android:layout_width="fill_parent"Android:layout_height="fill_parent"> <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"Android:layout_height="fill_parent"Android:text="This is a tab"/> <TextView Android:id="@+id/textview2"Android:layout_width="fill_parent"Android:layout_height="fill_parent"Android:text="This is another tab"/> <TextView Android:id="@+id/textview3"Android:layout_width="fill_parent"Android:layout_height="fill_parent"Android:text="This is a third tab"/> </FrameLayout> </LinearLayout></TabHost>
Explain the red label, we should not be able to change it.
Code for Activity class:
Public classMainactivity extends tabactivity{//declaring Tabhost objectsTabhost Mtabhost; /** Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); //Get Tabhost ObjectMtabhost =Gettabhost (); /*add tags to tabhost*/ //Create a new Newtabspec (NEWTABSPEC)//set its label and icon (Setindicator)//setting Content (setcontent)Mtabhost.addtab (Mtabhost.newtabspec ("tab_1"). Setindicator ("TAB 1", Getresources (). getdrawable (R.DRAWABLE.IMG1)). SetContent (R.id.textview1)); Mtabhost.addtab (Mtabhost.newtabspec ("tab_2"). Setindicator ("TAB 2", Getresources (). getdrawable (R.drawable.img2)). SetContent (R.ID.TEXTVIEW2)); Mtabhost.addtab (Mtabhost.newtabspec ("Tab_3"). Setindicator ("TAB 3", Getresources (). getdrawable (R.DRAWABLE.IMG3)). SetContent (R.ID.TEXTVIEW3)); //set the background color of TabhostMtabhost.setbackgroundcolor (Color.argb ( Max, A, -, Max)); //set the background image resource for TabhostMtabhost.setbackgroundresource (R.DRAWABLE.BG0); //sets which label is currently displayedMtabhost.setcurrenttab (0); //tag Switch event handling, SetontabchangedlistenerMtabhost.setontabchangedlistener (NewOntabchangelistener () {@Override Public voidontabchanged (String tabId) {}}); }}
Go to the core issue.
1. Change the icon for the Tabhost title.
First we should get to the layout view, then get the corresponding control, the icon should be ImageView, the text should be textview.
The code is as follows:
View MView = Mtabhost.gettabwidget (). Getchildat (0);//0 represents the first tabImageView ImageView= (ImageView) Mview.findviewbyid (Android. R.id.icon);//Get Control ImageViewImageView. Setimagedrawable (Getresources (). getdrawable (R.DRAWABLE.IMG3));//Change the icons we needImageView= (ImageView) mtabhost.gettabwidget (). Getchildat (1). Findviewbyid (Android. R.id.icon); ImageView. Setimagedrawable (Getresources (). getdrawable (R.drawable.img2)); ImageView= (ImageView) mtabhost.gettabwidget (). Getchildat (2). Findviewbyid (Android. R.id.icon); ImageView. Setimagedrawable (Getresources (). getdrawable (R.DRAWABLE.IMG1));
2. Change the title of the Tabhost.
TextView TextView = (TextView) mtabhost.gettabwidget (). Getchildat (0). Findviewbyid (Android. R.id.title); Textview.settextsize (1= (TextView) mtabhost.gettabwidget (). Getchildat (1 ). Findviewbyid (Android. R.id.title); Textview.settextsize (2= (TextView) mtabhost.gettabwidget (). Getchildat (2 ). Findviewbyid (Android. R.id.title); Textview.settextsize (3);
Actually get the view, we can then set the title of the color Ah, font ah, font size and so on.
Android Tabhost dynamically modify the icon or dynamically change the title