How do I create a tab inside a tab in Android?
When I tap a tab, how do I make there to be three more tabs available to tap?
I'm new to An开发者_高级运维droid, and I'm doing a simple project.
You can follow my sample code below (create controls on the fly). If you want to, you can also follow the logic below for use with xml layout.
package com.test;
import android.app.Activity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;
public class DynamicTabDemo extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
View view = createTab();
setContentView(view);
}
private android.view.ViewGroup createTab() {
TabHost tabHost = new TabHost(this);
tabHost.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
TabWidget tabWidget = new TabWidget(this);
tabWidget.setId(android.R.id.tabs);
tabHost.addView(tabWidget, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.setId(android.R.id.tabcontent);
frameLayout.setPadding(0, 65, 0, 0);
tabHost.addView(frameLayout, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
tabHost.setup();
TabSpec tabParent = tabHost.newTabSpec("Parent");
tabParent.setIndicator("Tab Parent");
tabParent.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
LinearLayout panel = new LinearLayout(DynamicTabDemo.this);
panel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
TabHost tabHost = new TabHost(getApplicationContext());
tabHost.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
TabWidget tabWidget = new TabWidget(getApplicationContext());
tabWidget.setId(android.R.id.tabs);
tabHost.addView(tabWidget, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
FrameLayout frameLayout = new FrameLayout(getApplicationContext());
frameLayout.setId(android.R.id.tabcontent);
frameLayout.setPadding(0, 65, 0, 0);
tabHost.addView(frameLayout, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
tabHost.setup();
TabSpec tabChild1 = tabHost.newTabSpec("Child_1");
tabChild1.setIndicator("Child 1");
tabChild1.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
TextView txt = new TextView(DynamicTabDemo.this);
txt.setText("Test Child 1");
txt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f);
txt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return txt;
}
});
tabHost.addTab(tabChild1);
TabSpec tabChild2 = tabHost.newTabSpec("Child_2");
tabChild2.setIndicator("Child 2");
tabChild2.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
TextView txt = new TextView(DynamicTabDemo.this);
txt.setText("Test Child 2");
txt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f);
txt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return txt;
}
});
tabHost.addTab(tabChild2);
TabSpec tabChild3 = tabHost.newTabSpec("Child_3");
tabChild3.setIndicator("Child 3");
tabChild3.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
TextView txt = new TextView(DynamicTabDemo.this);
txt.setText("Test Child 3");
txt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f);
txt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return txt;
}
});
tabHost.addTab(tabChild3);
panel.addView(tabHost);
return panel;
}
});
tabHost.addTab(tabParent);
return tabHost;
}
}
Hope it helps.
If you are using a TabActivity with your tab layout defined in XML, then simply make another TabActivity and other layout file, and in the first one, when you are making the intents, one of the intents would be the 2nd TabActivity.
You'd be better off making a
extends TabActivity
rather than
extends Activity
So for example, here is the code:
public class HelloTabWidget extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ArtistsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
The xml can be found in the below link also. Good resources on this type of design can be found here:
http://www.google.co.uk/search?q=create+tabs+in+android&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-GB:official&client=firefox-a
精彩评论