根据用于创建选项卡 UI的 android 开发人员文档,您需要有一个 TabHost 和 TabWidget,并且 TabHost 必须是布局的根节点。
一切都很完美,我尝试了这个例子,一切都很好。
就在查看选项卡的 API 示例时,我遇到了 tabs1.java ( http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/Tabs1.html )没有在布局中使用任何选项卡元素。
这是创建选项卡的示例工作代码,根本不使用任何布局。
public class HelloAndroid extends TabActivity implements TabHost.TabContentFactory {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1").setContent(this));
}
public View createTabContent(String tag) {
TextView text = new TextView(this);
text.setText("tab1");
return text;
}
}
谁能解释这是如何工作的?以及这与使用教程中解释的基于布局的方法有何不同。
谢谢。