The main.xml file should have the following. Elements like buttons or TextViews that are needed to be displayed outside of the tabs or tab window should be placed above or below TabHost. Use the include method to add tab content layout files.
main.xml
<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"
android:isScrollContainer="true">
<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">
</TabWidget>
android:layout_width="fill_parent"<FrameLayout android:id="@android:id/tabcontent"
android:layout_height="fill_parent"></FrameLayout>
</LinearLayout>I'm only going to include two tabs. You need to have a separate layout.xml file for each tab's content.
</TabHost>
tab1.xml and tab2.xml (they are the same for the sake of this tutorial)
xmlns:android="http://schemas.android.com/apk/res/android"<LinearLayout
android:id="@+id/scheduler_dayview"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<TextView android:layout_width="fill_parent"
android:padding="5px"android:text="Tab 1"
android:id="@+id/TextView01"
android:layout_height="wrap_content"
</TextView>
</LinearLayout>
Tab1.java and Tab2.java
public class Tab1 extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.tab1);
}
}
Now for the java class.
TabLayoutExample.Java
public class TabLayoutExample extends TabActivity {
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Tab Stuff
Resources res = getResources();TabHost tabHost = getTabHost();Intent intent;
// Create an Intent to launch an Activity for the tabintent = new Intent().setClass(this, Tab1.class);// Initialize a TabSpec for each tab and add it to the TabHosttabHost.addTab(tabHost.newTabSpec("t1").setIndicator("Tab1",res.getDrawable(R.drawable.tab1)).setContent(intent));
// Do the same for the other tabsintent = new Intent().setClass(this, Tab2.class);tabHost.addTab(tabHost.newTabSpec("t2").setIndicator("Tab2",res.getDrawable(R.drawable.tab2)).setContent(intent));
tabHost.setCurrentTab(0);}
}
No comments:
Post a Comment