The TabWidget itself does not do much except contain a list of tab
labels that exist within the parent TabHost.
Basically it is nothing but a set of labels that the user clicks
in order to select a specific tab. The actual content of each tab
is to be held by the 2nd object in the TabHost i.e. a FrameLayout.
Hence both the TabWidget and FrameLayout need to be present on each tab.
And in order to align htme one after another,we embed them into a LinearLayout.
First you have to create separate activities for each tab. Here I have 3 tabs.
The 3 activities are AndroidActivity, WindowActivity, and
IphoneActivity.
Each one displays a simple message using TextView.
public class AndroidActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.android_layout); } } public class WindowActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.window_layout); } } public class IphoneActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.iphone_layout); } }
you may defined here there tab:-
androidtab.xml
iphonetab.xml
|
Here is the main layout file, named main.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"/> </LinearLayout> </TabHost>
Now main Java File, looks like this types, you can edit what types
you want to add your apps
public class TabLayoutExample extends TabActivity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Resources res = getResources();TabHost tabHost = getTabHost();TabHost.TabSpec spec;Intent intent;intent = new Intent().setClass(this, MusicActivity.class);spec = tabHost.newTabSpec("Android").setIndicator("Android",res.getDrawable(R.drawable.androidtab)).setContent(intent);tabHost.addTab(spec);intent = new Intent().setClass(this, VideoActivity.class);spec = tabHost.newTabSpec("Window").setIndicator("Window",res.getDrawable(R.drawable.windowtab)).setContent(intent);tabHost.addTab(spec);intent = new Intent().setClass(this, FavoriteActivity.class);spec = tabHost.newTabSpec("Iphone").setIndicator("Iphone",res.getDrawable(R.drawable.iphonetab)).setContent(intent);tabHost.addTab(spec);tabHost.setCurrentTab(2);}}
At last but not the least, dont forget to add to mainfeast all tab , like this.
<activity android:label="@string/app_name" android:name=".AndroidActivity" > </activity><activity android:label="@string/app_name" android:name=".WindowActivity" > </activity><activity android:label="@string/app_name" android:name=".IphoneActivity" >
</activity>
Hop you guys helpful this short article. source form www.developer.android.com
Video Tutorials Here:
0 comments
Thanks for your comment