不使用布局创建选项卡式 UI
根据创建选项卡 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;
}
}
谁能解释一下这是如何工作的?这与使用教程中解释的基于布局的方法有何不同。
谢谢。
As per the android developer docs for creating tab UI you need to have a TabHost and TabWidget and the TabHost must be the root node for the layout.
All perfect, I tried the example and everything's fine.
Just while looking at the API Samples of tabs, I came across tabs1.java (http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/Tabs1.html) which was not using any tab elements in the layout.
Here is the sample working code that creates a tab, without using any layout at all.
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;
}
}
Can anyone explain that how this is working ? And how this is different that using the Layout based approach as explained in the tutorial.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为 TabActivity 以编程方式创建 TabHost 布局。
您可以检查 http://www.google.com/codesearch/p?hl=en#uX1GffpyOZk/core/java/ android/app/TabActivity.java&q=TabActivity&sa=N&cd=1&ct=rc
This is because TabActivity programatically creates a TabHost layout.
You can check http://www.google.com/codesearch/p?hl=en#uX1GffpyOZk/core/java/android/app/TabActivity.java&q=TabActivity&sa=N&cd=1&ct=rc