Android ApiDemos示例解析(194):Views->Tabs->Content By Factory
本例使用TabHost.TabContentFactory 动态为Tab页面创建Content,使用TabHost.TabContentFactory可以按需(用户选择某个页面后)动态创建Tab页面内容。
方法是使用TabActivity实现TabHost.TabContentFactory接口并实现createTabContent方法:
[java]
public class Tabs2 extends TabActivity
implements TabHost.TabContentFactory {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("tab1", getResources()
.getDrawable(R.drawable.star_big_on))
.setContent(this));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("tab2")
.setContent(this));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("tab3")
.setContent(this));
}
/** {@inheritDoc} */
public View createTabContent(String tag) {
final TextView tv = new TextView(this);
tv.setText("Content for tab with tag " + tag);
return tv;
}
}
public class Tabs2 extends TabActivity
implements TabHost.TabContentFactory {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("tab1", getResources()
.getDrawable(R.drawable.star_big_on))
.setContent(this));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("tab2")
.setContent(this));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("tab3")
.setContent(this));
}
/** {@inheritDoc} */
public View createTabContent(String tag) {
final TextView tv = new TextView(this);
tv.setText("Content for tab with tag " + tag);
return tv;
}
}
本例为每个页面动态创建一个TextView作为Tab的Content 。
补充:移动开发 , Android ,