1. Do not inherit TabActivity
2. Define TabHost in the layout
Note: The TabWidget id must be @ android: id/tabs, and the FrameLayout id must be @ android: id/tabcontent.
Java code:
Package yc. demo;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. TabHost;
Import android. widget. TabHost. TabSpec;
Public class TabhostDemo1Activity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. demo1 );
// Obtain the TabHost
TabHost tabHost = (TabHost) this. findViewById (R. id. tabs );
TabHost. setup ();
// Obtain a new TabHost. TabSpec and associate it with the current tabHost.
// Parameter: Required tab labels
TabSpec pSpes = tabHost. newTabSpec ("parent ");
PSpes. setIndicator ("parent class", this. getResources (). getDrawable (R. drawable. msg_icon ));
PSpes. setContent (R. id. textView1 );
TabSpec subSpec = tabHost. newTabSpec ("sub ");
SubSpec. setIndicator ("subclass", this. getResources (). getDrawable (R. drawable. at_icon ));
SubSpec. setContent (R. id. textView2 );
// Add a tab to tabHost
TabHost. addTab (pSpes );
TabHost. addTab (subSpec );
}
}
Xml layout file:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<TabHost android: id = "@ + id/tabs"
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_height = "fill_parent"
Android: layout_width = "fill_parent">
<TextView android: id = "@ + id/textView1"
Android: layout_height = "fill_parent"
Android: layout_width = "fill_parent"
Android: text = "parent"/>
<TextView android: id = "@ + id/textView2"
Android: layout_height = "fill_parent"
Android: layout_width = "fill_parent"
Android: text = "sub"/>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Tip: The display order of tabs is determined based on the order in which you add tabs.
From tianshijianbing1989