不使用布局创建选项卡式 UI

发布于 2024-09-06 22:23:33 字数 1154 浏览 1 评论 0原文

根据创建选项卡 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文