Вы можете следовать моему примеру кода ниже (создавать элементы управления на лету).При желании вы также можете следовать приведенной ниже логике для использования с макетом XML.
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;
}
}
Надеюсь, это поможет.